Python Lists

Descriptions

Lists are more flexible than variables and can contain heterogeneous elements, not just alphanumeric characters.

Lists also have nestable elements, this elements can be lists, which means that you can have other multiple data types inside a list.

They are also mutable which means you can change the type.

A list is form using brackets [ ] and separate the elements using a comma.

>>> a = [ 'a', 'b', 'c', 1, 2, 3]
>>> b = [ a, 6]
>>> print b[0]
[ 'a', 'b', 'c', 1, 2, 3]
>>> print a[3]
1

You can even have lists inside lists!

>>> x = [ [ 0, 1, 'z' ], 'yay' ]
>>> print x[0]
[0, 1, 'z']

What happened there is it called the first object in the list which just happened to be a list so that is what it printed. So what if you want to get the 'z' out of that embeded list? Here's how you do it:

>>> print x[0][2]
z

All you do is add the number of the object you want called in the embeded list after specifying the list it is held in.

Now what if you want that 'yay' to say 'boo'? Simply change it on the fly:

>>> x[1] = 'boo'
>>> print x
[[0, 1, 'z'], 'boo']