learn code with examples

Python List of Lists

Creating List of Lists

To create list of lists in python, each element of outer list will again be the list. This is also called Nested List or Two Dimensional List.

List is created with [ ] and each element is specified within it. Instead of specifying basic data type (int, float, string), lets have another list ['a', 'b', 'c', 'd'] as first element. This will create list of lists.

This can be specified as [ ['a', 'b', 'c', 'd'] ] .

Pls refer below example-2, you can have list variable (a2), gets assigned as element of some other list (a1).

# Example, List of Lists.
a1 = [ ['a', 'b', 'c', 'd'] ]

#Example-2, How to assign list as element to some other list.
a2 = ['a', 'b', 'c', 'd']
a1 = [ a2 ]

Create list of lists using loop

Below example shows how to create list at run time and append it to outer_list.

First, we created outer_list as empty list.
Then, inner_list is created in loop. Each time, it create list of two elements and append it to outer_list.
# Example, Create list of lists using loop
outer_list = []
for x in range(5):
  inner_list = list(range(x,x+2))

print(outer_list) # [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]

Understanding how values assigned in list of lists

In above example, variable a1 does not keep track of a2, Rather, value of a2 is assigned within a1. So, lets change value of a2, and see, if value in a1 changes?

# Example, Changing values in list of lists
a2 = ['a', 'b', 'c', 'd']
a1 = [ a2 ]

a2 = [1,2,3,4]
print(a1) # Values in a1 does not change, if, a2 variable is modified.

Accessing element in List of Lists

Using python indexing, specific element can be accessed in list of lists.

In simple list, indexing refers to element. But, in list of lists, first indexing will refer to outer list, Hence, output of first indexing will be inner list itself.

# Example, Accessing element in list of lists
a2 = ['a', 'b', 'c', 'd']
a3 = [1,2,3,4]
a1 = [ a2, a3 ]

print(a1[0]) # prints first element of a1, i.e. ['a', 'b', 'c', 'd']
print(a1[1]) # prints second element of a1, i.e. [1,2,3,4]

Say, you want to access specific element in second list (index = 1 of outer list), a1[1] is referring to desired list. Now, you have to use indexing again on inner list.

# Example, Accessing element in list of lists
a2 = ['a', 'b', 'c', 'd']
a3 = [1,2,3,4]
a1 = [ a2, a3 ]

print(a1[1][3]) # prints 4th element in a1[1]
print(a1[0][3]) # prints 4th element in a1[0]

Two Dimensional List, 2-D List

List of Lists can be termed as two dimensional list. In such case, Python does not define row-column mechanism. But, we can term outer list as list of rows, i.e. each entry in outer list is row. In below example, a1[0] refers to first row, etc.

Pls note that specific row can have any number of elements (i.e. column size is not fixed).

# Example, Two Dimensional List
a1 = [ ['a', 'b', 'c', 'd'], [1,2,3,4], ['i', 'ii', 'iii', 'iv', 'v'] ]

List of Lists Vs Matrix

Generally Matrix has m x n - fixed size, however, list of lists has dynamic size for each row.

Updating value in list of lists

Any value can be updated using list indexing. Please refer below example of updating value in 2nd row, 2nd column. Lets correct the row defined as 1,1,3,4 to 1,2,3,4.

# Example, Updating value in list of lists
a1 = [ ['a', 'b', 'c', 'd'], [1,1,3,4], ['i', 'ii', 'iii', 'iv', 'v'] ]

print(a1[1]) # We need [1,2,3,4]
a1[1][1] = 2

Append in list of lists

In List of lists, Append works similar to regular list. Pls refer below example. To append element in outer list, you have to use append function on main list (i.e. list of lists - here a1). To append element in inner list, you have to use append function on specific element of outer list.

# Example, Append value in list of lists
a1 = [ ['a', 'b', 'c', 'd'], [1,2,3,4], ['i', 'ii', 'iii', 'iv', 'v'] ]

a1.append( ['w', 'x', 'y', 'z'] )

Concatenate list of lists

List comprehension can be used to concatenate list of lists.

In below example, there are two for loops. First For loop iterate on outer_list and access each inner_list. Second for loop iterate on inner_list and access each element as variable x. List comprehension concatenate each elemen x.

# Example, Concatenate list of lists
outer_list = [ ['a', 'b', 'c', 'd'], [1,2,3,4], ['i', 'ii', 'iii', 'iv', 'v'] ]

result = [x for inner_list in outer_list for x in inner_list]

In this page...

Copyright © All rights reserved.