Prev: List slicing | Next: List functions & methods

- When
**the items of a list are lists themselves**then we have '**list of lists**' - Remember: a list is a heterogeneous object; it can contain lists or objects of any other type.

- L1 is a list-of-lists; it contains three items which are simple lists of integers

In [3]:

```
L1 = [[1,2,3], [4], [5,6]]
print(L1)
print(L1[1])
```

- L2 offers an example of heterogeneity in lists: it has three elements:
- a list, an integer and a string

In [4]:

```
L2 = [[1,20], 200, 'A']
print(L2)
print(L2[0])
```

- When all items of a list are lists we can conveniently talk of
**2D lists**(two-dimensional lists)

... but remember that this is only for convenience;*dimensionality is not really a property of lists*

- In the following example L1 is a
**square 2D list**: consists of*N sublists with N single elements each*(in the example N=3)

In [7]:

```
L1 = [[1,2,3],[4,5,6],[7,8,9]]
print(L1)
print()
#printing L1 in pseudo "rows x columns" format
for row in L1: #No index is required!
print(row)
```

- L2 is a
**rectangular 2D list**: contains N sublists with M single elements each (in the example N=4 ("rows-like"), M=3 ("columns-like"))

In [9]:

```
L2 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
print(L2)
print()
#printing L2 in pseudo "rows x columns" format
for row in L2: # No index is required!
print(row)
```

- L3 is a
**ragged 2D list**: contains N sublists of different length each (in the example N=4)

In [20]:

```
L3 = [[1,2,3],[4],[7,8],[10,11,12,13,14]]
print(L3)
print()
#printing L3 in pseudo "rows x columns" format
for row in L3: #No index is required!
print(row)
```

In [10]:

```
a = [ [ 1, 2, 3 ] , [ 4, 5, 67 ] ]
for i in a:
print(i)
```

- List comprehension is the most elegant, concize and programmatically faster way to create 2D lists
- In case of 2D lists comprehension we will need
**2 iterator variables and 2 loops (inner and outer)**to describe the list values

In [11]:

```
alist = [[0 for i in range(5)] for k in range(3)]
alist
```

Out[11]:

The above comprehension works as follows:

- The
*inner loop*'0 for i in range(5)' defines a single list object with five zeros - The
*outer loop*'for k in range(3)' defines that 3 elements are created; each one of the shape described in the inner loop

- So we get a 3 X 5 list

In [12]:

```
for x in alist:
print(x)
```

- See some more examples

- 4 X 2 rectangular list with '1'

In [15]:

```
blist = [[1 for i in range(2)] for k in range(4)]
for x in blist:
print(x)
```

- 'ragged' list with 4 elements of varied length

In [19]:

```
clist = [[i+1 for i in range(2)] if k%2==0 else [i+3 for i in range(3)] for k in range(4)]
for row in clist:
print(row)
```

- Obviously you need two indexes to access a single element in a 2D list

- In the following example:
- index [1] refers to the second item of L2 which is the [4,5,6] list
- double index [1][2] refers to the third item of the L1[1] list

In [21]:

```
L1 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
print(L1[1])
print(L1[1][2])
#print(blist[1,2]) #this syntax is not accepted for indexing 2D lists
```

- Below:
- L2[1][2] returns the 3rd item (index 2) of the L2[1] item
- L2[1] returns the 2nd (index 1) item of L2
- L2[1:2] returns a slice containing only the 2nd (index 1) item of L2

In [22]:

```
L2 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
print(L2[1][2])
print(L2[1])
print(L2[1:2])
```

- We can go on and create lists of any
*pseudo-dimensionality*we like - The following comprehension returns a "3D list" having as items two square 2D lists of 4X4 shape each

In [24]:

```
a3dlist = [[[1 if k==n else 0 for k in range(4)] for n in range(4)] for m in range(2)]
for _2d in a3dlist:
for row in _2d:
print(row)
print()
```

- To better understand how the comprehension works:
- Begin from the
*inner loop*(k iterator) that defines a 4-item list with values depending on the conditional expression: "1 if k==n else 0" ... - ..then move to the
*middle loop*(n iterator) which describes 4 items of the 'inner loop shape'; so far a 2D list of 4X4 shape has been described.. - ..and finally move to the
*outer loop*(m iterator) which defines that two elements of 4X4 shape are constructed

- Begin from the

... when seriously working with data you are most likely to use advanced structures like '**nd-array**' (in numpy package) and/or '**DataFrame**' (in pandas)

. Free learning material

. See full copyright and disclaimer notice