Hi there Serdar Yegulalp here with Infoworld at IDG. Today
in Smart Python we'll be looking at the basics of the list collection type
now a list in python is a collection of objects in a one-dimensional container
just a bunch of python objects all in a row
lists are best when you want to store things in a specific order
when you want to iterate through them in that order or in the reverse order
and when you want to access them by their position in the list called an
index or when you want to get a random item
from the list now lists don't all have to contain the
same kind of object it's often useful for your own
convenience if you do but there's nothing that says they must as you can
see here a list can contain any kind of python
object including functions or module objects if it's a python object it'll
fit in a list to access an item in a list you use the
items index this is a number starting at zero so
zero always fetches the first item for the second item you use index one
and so on and so forth if you want to start from the end of the
list and work backwards use a negative index this way you can
always get the last item by just using -1 as the index
there's also more sophisticated indexing options for lists called slicing but the
slicing syntax is worth the whole video unto itself so we'll skip that for now
now we hinted earlier that lists can also contain other lists
if you want to extract something from a list within a list
just put one index after the next the name of the list object and its
index extracts the sub-list and then the next index extracts the
item itself from that sub list now one common thing
done with lists is iteration this is when we traverse an entire list
and do something with each element in it python has a common way to do this with
any type of object that can be iterated through
it's called the for syntax if we say for element in my list
the code block that follows will take each item in the list
under the name element and let us perform some operation with it
we can also use the enumerate function with the list
to step through the list and give us the index number of the current object along
with the object itself lists have several built-in commands or
methods that let you perform operations on the
list append adds an element to the end of the list
and pop removes and returns the last element
from a list this makes it easy to build stack like
structures with lists since appending and popping are very fast
you can also use insert along with a location
to indicate an index point in the list where you want to add an item
and you can use pop with an index to pop an element
from a specific index on the list now a note about inserting or removing if you
insert or remove from the list anywhere but the end of the list that's
a potentially a much slower operation than just
adding or removing from or to the end of the list
so if you're dealing with a list that has thousands or tens of thousands or
hundreds of thousands of items and you're constantly adding elements in the
middle or at the beginning or you're popping
items out of the middle or the beginning you're going to quickly find that things
will be much slower than if you're just adding to or removing from the end
there's another object in python called the double ended queue
that is more worth using in these scenarios but we'll cover that in a
different video as well now you could also test for the presence
of a given item in a list by simply using the syntax if
item in list but be warned that finding items in a
list by anything other than their index can get very slow
when you say if item in list the only way python has to find the item is by
traversing the entire list checking each item and quitting the
minute it finds the item in question now for really big collections of
objects or many such searches repeated over and over again in a type loop
the search time can really add up so to that end the best way to test for
membership the presence of an object in a
collection is to use a dictionary or a set
rather than a list now that's it for this basic intro to python lists we've
got more videos diving deeper into the subject and how they're useful coming
real soon if you like this video leave a comment
below don't forget to subscribe to the idg techtalk channel on youtube and for
more smart python videos be sure to follow us on facebook youtube and
infoworld.com