More on Lists in Python¶

Heterogenous lists¶

So far, we only saw lists that contained one type of value. For example, we saw lists of integers, or lists of strings. In Python lists can be heterogenous -- they can contain multiple types of values. Here is a simple example

In [1]:
L = [42, 43, "hello", 44]
print("L[0] ==", L[0], "and L[2] ==", L[2])
L[0] == 42 and L[2] == hello

Heterogenous lists and functions¶

Here is a more interesting example -- let's define a function

In [2]:
def f(n):
    return n + 42

If we go simply f, rather than f(5) for example, Python will just tell us that f is a function:

In [3]:
f
Out[3]:
<function __main__.f>

We can have f be an element of the list L:

In [4]:
L = [2, 4, 7, f, 6]
L[3]
Out[4]:
<function __main__.f>

L[3] evaluates to f in the same way that L[0] would evaluate to 2. We can, of course, call f by referring to it as L[3]:

In [5]:
L[3](8)
Out[5]:
50

Of course, we didn't have to do anything as complicated as that in order to refer to f by another name:

In [6]:
g = f
g(10)
Out[6]:
52

Lists of lists¶

Here's another example of complex objects' being elements of lists:

In [7]:
L = [42, 43, [45, 46], 47]
print(L[2])
[45, 46]

L[2] is the list [45, 46]. We can access 46 using:

In [8]:
L[2][1]
Out[8]:
46

L[2] evaluates to the list [45, 46], and then L[2][1] is the second element of L[2]. Note that the following works similarly:

In [9]:
[45, 46][1]
Out[9]:
46

Tables as lists of lists¶

Here is a usual situation: we want to define a table. We can do this by creating a list of lists (i.e., a list whose elements are lists). We can think of it as a list of rows.

In [10]:
L = [[1, 2, 3, 4],
     [1, 0, 1, 0],
     [2, 2, 3, 5]]

We typeset things so that it's apparent that L represents a table. However, as far as Python is concerned, L is just an orindary list of lists.

In [11]:
L
Out[11]:
[[1, 2, 3, 4], [1, 0, 1, 0], [2, 2, 3, 5]]

Now, how do we access the 5 in the third row? To get the third row, we go

In [12]:
L[2]
Out[12]:
[2, 2, 3, 5]

To get the entry 5 at the end of the row, we need the element at index 3:

In [13]:
L[2][3]
Out[13]:
5

Printing out the table¶

Here is an easy (if not ideal) way to display L as a table:

In [14]:
for row in L:
    print(row)
[1, 2, 3, 4]
[1, 0, 1, 0]
[2, 2, 3, 5]

row is first the first list, then the second list, and so on. Notw that we had to do this because Python by default doesn't print L is if it were a table:

In [15]:
print(L)
[[1, 2, 3, 4], [1, 0, 1, 0], [2, 2, 3, 5]]

Nested list silliness¶

Consider the following:

In [16]:
L = [[[[[]]]]]

L is a list of length 1, with just one element:

In [17]:
L[0]
Out[17]:
[[[[]]]]

We can go further:

In [18]:
L[0][0]
Out[18]:
[[[]]]
In [19]:
L[0][0][0]
Out[19]:
[[]]
In [20]:
L[0][0][0][0]
Out[20]:
[]

That's as far as we can go:

In [21]:
L[0][0][0][0][0]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-21-fa867e755bc3> in <module>()
----> 1 L[0][0][0][0][0]

IndexError: list index out of range

You cannot access the first element of [], since [] contains no elements at all.