In [1]:

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

L[0] == 42 and L[2] == hello

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

`f`

by another name:

In [6]:

```
g = f
g(10)
```

Out[6]:

52

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

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]]
```

`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

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]]

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]
```

You cannot access the first element of `[]`

, since `[]`

contains no elements at all.