×

Nested Lists in Python

Nested lists are Python representations of two dimensional arrays. They are used to represent lists of lists. For example, a list of grocery lists for the month or matrices we can multiply. In this post we’re going to go over how to use Python to create and manipulate nested lists.

Python Nested Lists

The easiest way to create a nested list in Python is simply to create a list and put one or more lists in that list. In the example below we’ll create two nested lists.

First, we’ll create a nested list by putting an empty list inside of another list. Then, we’ll create another nested list by putting two non-empty lists inside a list, separated by a comma as we would with regular list elements.

# create a nested list
nlist1 = [[]]
nlist2 = [[1,2],[3,4,5]]

List Comprehension with Nested Lists

We can also create nested lists with list comprehension. List comprehension is a way to create lists out of other lists. In our example below, we’ll create two lists with list comprehension in two ways.

First we’ll create a nested list using three separate list comprehensions. Second, we’ll create a nested list with nest list comprehension.

# create a list with list comprehension
nlist_comp1 = [[i for i in range(5)], [i for i in range(7)], [i for i in range(3)]]
nlist_comp2 = [[i for i in range(n)] for n in range(3)]
print(nlist_comp1)
print(nlist_comp2)

The results of the two lists should be: 

[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2]]
[[], [0], [0, 1]]

Adding Lists to a Two Dimensional Array

Now that we’ve learned how to create nested lists in Python, let’s take a look at how to add lists to them. We work with nested lists the same way we work with regular lists. We can add an element to a nested list with the append() function. In our example, we create a list and append it to one of our existing lists from above.

# append a list
list1 = [8, 7, 6]
nlist_comp1.append(list1)
print(nlist_comp1)

This should result in this list: [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2], [8, 7, 6]]

Concatenating Two Dimensional Lists in Python

Other than adding a list to our 2D lists, we can also add or concatenate two nested lists together. List concatenation in Python is quite simple, all we need to do is add them with the addition sign. Adding nested lists works the same way as adding plain, unnested lists. In our example, we’ll add the two lists we created using list comprehension together.

# concat nested lists
concat_nlist = nlist_comp1 + nlist_comp2
print(concat_nlist)

The list we should see from this is: [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2], [8, 7, 6], [], [0], [0, 1]]

How to Reverse a Nested List

Now that we’ve created, added to, and concatenated nested lists, let’s reverse them. There are multiple ways to reverse nested lists, including creating a reversed copy or using list comprehension. In this example though, we’ll reverse a list in place using the built-in reverse() function. 

# reverse a nested list
concat_nlist.reverse()
print(concat_nlist)

This should print out the nested list: [[0, 1], [0], [], [8, 7, 6], [0, 1, 2], [0, 1, 2, 3, 4, 5, 6], [0, 1, 2, 3, 4]]

Reversing the Sub Elements of a Nested List

Okay, so we can easily reverse a list using the reverse function. What if we want to reverse the sub elements of a nested list? We can reverse each of the lists in a nested list by looping through each list in the nested list and calling the reverse function on it.

# reverse sub elements of nested list
for _list in concat_nlist:
   _list.reverse()
print(concat_nlist)

If we call the above code on the original concatenated list, we will see this list: [[4, 3, 2, 1, 0], [6, 5, 4, 3, 2, 1, 0], [2, 1, 0], [6, 7, 8], [], [0], [1, 0]]

Reverse the Sub Elements and the Elements of a 2D Python Array

Now we can reverse the elements in a 2D list as well as reverse the elements of each nested list, we can put them together. To reverse the sub elements and the elements of a 2D list in Python, all we do is loop through each of the inside lists and reverse them, and then reverse the outside list after the loop. We can also do it in the reverse order.

# reverse sub elements + elements of a nested list
for _list in concat_nlist:
   _list.reverse()
concat_nlist.reverse()
print(concat_nlist)

Running this on the original concat_nlist should give: [[1, 0], [0], [], [6, 7, 8], [2, 1, 0], [6, 5, 4, 3, 2, 1, 0], [4, 3, 2, 1, 0]]

Turning a 2D List into a Normal List

So far, we’ve learned how to create, add to and together, and reverse a two-dimensional list in Python. Now, let’s take a look at turning that 2D list into a normal or flattened list. In this example, we’ll use list comprehension to extract each element from each sublist in the list.

# flatten list
flat_list = [ele for sublist in concat_nlist for ele in sublist]
print(flat_list)

When running the above code to flatten a list on the original concatenated lists, we should get this list: [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 8, 7, 6, 0, 0, 1]

Reverse a Flattened Nested List

Let’s put it all together. We just flattened our 2D Python array into a one-dimensional list. Earlier, we reversed our 2D list. Now, let’s reverse our flattened list. Just like with the 2D list, all we have to do to reverse our flattened list is run the reverse function on the flattened list.

# reverse elements of a flattened list
flat_list.reverse()
print(flat_list)

After running the reverse function on the list we flattened above, we should get: [1, 0, 0, 6, 7, 8, 2, 1, 0, 6, 5, 4, 3, 2, 1, 0, 4, 3, 2, 1, 0]

Summary of Nested Lists in Python

In this post about nested lists in Python we learned how to create, manipulate, and flatten nested lists. First we learned how to simply create nested lists by just putting lists into a list, then we learned how to create nested lists through list comprehension. 

Next, we learned how to do some manipulation of 2D arrays in Python. First, how to append a list, then how to concatenate two lists, and finally, how to reverse them. Lastly, we learned how to flatten a 2D list and reverse it.