Python List of Lists – A Helpful Illustrated Guide to Nested Lists in Python

There’s an element of confusion regarding the term “lists of lists” in Python. I wrote this most comprehensive tutorial on list of lists in the world to remove all those confusions by beginners in the Python programming language. This multi-modal tutorial consists of:

  • Source code to copy&paste in your own projects.
  • Interactive code you can execute in your browser.
  • Explanatory text for each code snippet.
  • Screencapture videos I recorded to help you understand the concepts faster.
  • Graphics and illustrated concepts to help you get the ideas quickly.
  • References to further reading and related tutorials.

So, if you’re confused by lists of lists, read on—and resolve your confusions once and for all!

What’s a List of Lists?

Definition: A list of lists in Python is a list object where each list element is a list by itself. Create a list of list in Python by using the square bracket notation to create a nested list [[1, 2, 3], [4, 5, 6], [7, 8, 9]].

Do you want to develop the skills of a well-rounded Python professional—while getting paid in the process? Become a Python freelancer and order your book Leaving the Rat Race with Python on Amazon (Kindle/Print)!

Leaving the Rat Race with Python Book

Interactive Example Python List of Lists

Let’s dive into a detailed example of a list of lists. Try it yourself in our interactive Python shell by clicking on the “run” button.

Puzzle: Can you figure out the output before executing the script?

Memory Analysis

It’s important that you understand that a list is only a series of references to memory locations. By playing with the code visualizer, you’ll gain a deeper understanding of how Python works at its core:

Simply click the “Next” button to see how each line of code unfolds.

Create a List of Lists in Python

Create a list of lists by using the square bracket notation. For example, to create a list of lists of integer values, use [[1, 2], [3, 4]]. Each list element of the outer list is a nested list itself.

Convert List of Lists to One List

Say, you want to convert a list of lists [[1, 2], [3, 4]] into a single list [1, 2, 3, 4]. How to achieve this? There are different options:

  • List comprehension [x for l in lst for x in l] assuming you have a list of lists lst.
  • Unpacking [*lst[0], *lst[1]] assuming you have a list of two lists lst.
  • Using the extend() method of Python lists to extend all lists in the list of lists.

Find examples of all three methods in the following code snippet:

lst = [[1, 2], [3, 4]]

# Method 1: List Comprehension
flat_1 = [x for l in lst for x in l]

# Method 2: Unpacking
flat_2 = [*lst[0], *lst[1]]

# Method 3: Extend Method
flat_3 = []
for l in lst:
    flat_3.extend(l)


## Check results:
print(flat_1)
# [1, 2, 3, 4]

print(flat_2)
# [1, 2, 3, 4]

print(flat_3)
# [1, 2, 3, 4]

Due its simplicity and efficiency, the first list comprehension method is superior to the other two methods.

Convert List of Lists to Dictionary

For some applications, it’s quite useful to convert a list of lists into a dictionary.

  • Databases: List of list is table where the inner lists are the database rows and you want to assign each row to a primary key in a new dictionary.
  • Spreadsheet: List of list is two-dimensional spreadsheet data and you want to assign each row to a key (=row name).
  • Data Analytics: You’ve got a two-dimensional matrix (=NumPy array) that’s initially represented as a list of list and you want to obtain a dictionary to ease data access.

There are three main ways to convert a list of lists into a dictionary in Python (source):

  1. Dictionary Comprehension
  2. Generator Expression
  3. For Loop

Let’s dive into each of those.

1. Dictionary Comprehension

Problem: Say, you’ve got a list of lists where each list represents a person and consists of three values for the person’s name, age, and hair color. For convenience, you want to create a dictionary where you use a person’s name as a dictionary key and the sublist consisting of the age and the hair color as the dictionary value.

Solution: You can achieve this by using the beautiful (but, surprisingly, little-known) feature of dictionary comprehension in Python.

persons = [['Alice', 25, 'blonde'],
           ['Bob', 33, 'black'],
           ['Ann', 18, 'purple']]

persons_dict = {x[0]: x[1:] for x in persons}
print(persons_dict)
# {'Alice': [25, 'blonde'],
#  'Bob': [33, 'black'],
#  'Ann': [18, 'purple']}

Explanation: The dictionary comprehension statement consists of the expression x[0]: x[1:] that assigns a person’s name x[0] to the list x[1:] of the person’s age and hair color. Further, it consists of the context for x in persons that iterates over all “data rows”.

Exercise: Can you modify the code in our interactive code shell so that each hair color is used as a key and the name and age are used as the values?

Modify the code and click the “run” button to see if you were right!

2. Generator Expression

A similar way of achieving the same thing is to use a generator expression in combination with the dict() constructor to create the dictionary.

persons = [['Alice', 25, 'blonde'],
           ['Bob', 33, 'black'],
           ['Ann', 18, 'purple']]

persons_dict = dict((x[0], x[1:]) for x in persons)
print(persons_dict)
# {'Alice': [25, 'blonde'],
#  'Bob': [33, 'black'],
#  'Ann': [18, 'purple']}

This code snippet is almost identical to the one used in the “list comprehension” part. The only difference is that you use tuples rather than direct mappings to fill the dictionary.

3. For Loop

Of course, there’s no need to get fancy here. You can also use a regular for loop and define the dictionary elements one by one within a simple for loop. Here’s the alternative code:

persons = [['Alice', 25, 'blonde'],
           ['Bob', 33, 'black'],
           ['Ann', 18, 'purple']]

persons_dict = {}
for x in persons:
    persons_dict[x[0]] = x[1:]

print(persons_dict)
# {'Alice': [25, 'blonde'],
#  'Bob': [33, 'black'],
#  'Ann': [18, 'purple']}

Again, you map each person’s name to the list consisting of its age and hair color.

Convert List of Lists to NumPy Array

Problem: Given a list of lists in Python. How to convert it to a 2D NumPy array?

Example: Convert the following list of lists

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

into a NumPy array

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

Solution: Use the np.array(list) function to convert a list of lists into a two-dimensional NumPy array. Here’s the code:

# Import the NumPy library
import numpy as np

# Create the list of lists
lst = [[1, 2, 3], [4, 5, 6]]

# Convert it to a NumPy array
a = np.array(lst)

# Print the resulting array
print(a)
'''
[[1 2 3]
 [4 5 6]]
'''

Try It Yourself: Here’s the same code in our interactive code interpreter:

<iframe height="700px" width="100%" src="https://repl.it/@finxter/numpylistoflists?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>

Hint: The NumPy method np.array() takes an iterable as input and converts it into a NumPy array.

Convert a List of Lists With Different Number of Elements

Problem: Given a list of lists. The inner lists have a varying number of elements. How to convert them to a NumPy array?

Example: Say, you’ve got the following list of lists:

[[1, 2, 3], [4, 5], [6, 7, 8]]

What are the different approaches to convert this list of lists into a NumPy array?

Solution: There are three different strategies you can use. (source)

(1) Use the standard np.array() function.

# Import the NumPy library
import numpy as np

# Create the list of lists
lst = [[1, 2, 3], [4, 5], [6, 7, 8]]

# Convert it to a NumPy array
a = np.array(lst)

# Print the resulting array
print(a)
'''
[list([1, 2, 3]) list([4, 5]) list([6, 7, 8])]
'''

This creates a NumPy array with three elements—each element is a list type. You can check the type of the output by using the built-in type() function:

>>> type(a)
<class 'numpy.ndarray'>

(2) Make an array of arrays.

# Import the NumPy library
import numpy as np

# Create the list of lists
lst = [[1, 2, 3], [4, 5], [6, 7, 8]]

# Convert it to a NumPy array
a = np.array([np.array(x) for x in lst])

# Print the resulting array
print(a)
'''
[array([1, 2, 3]) array([4, 5]) array([6, 7, 8])]
'''

This is more logical than the previous version because it creates a NumPy array of 1D NumPy arrays (rather than 1D Python lists).

(3) Make the lists equal in length.

# Import the NumPy library
import numpy as np

# Create the list of lists
lst = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]

# Calculate length of maximal list
n = len(max(lst, key=len))

# Make the lists equal in length
lst_2 = [x + [None]*(n-len(x)) for x in lst]
print(lst_2)
# [[1, 2, 3, None], [4, 5, None, None], [6, 7, 8, 9]]

# Convert it to a NumPy array
a = np.array(lst_2)

# Print the resulting array
print(a)
'''
[[1 2 3 None]
 [4 5 None None]
 [6 7 8 9]]
'''

You use list comprehension to “pad” None values to each inner list with smaller than maximal length. See the original article on this blog for a more detailed version of this content.

Convert List of Lists to Dataframe

Problem: You’re given a list of lists. Your goal is to convert it into a Pandas Dataframe.

Example: Say, you want to compare salary data of different companies and job descriptions. You’ve obtained the following salary data set as a list of list:

salary = [['Google', 'Machine Learning Engineer', 121000],
          ['Google', 'Data Scientist', 109000],
          ['Google', 'Tech Lead', 129000],
          ['Facebook', 'Data Scientist', 103000]]

How can you convert this into a Pandas Dataframe?

Solution: The straight-forward solution is to use the pandas.DataFrame() constructor that creates a new Dataframe object from different input types such as NumPy arrays or lists.

Here’s how to do it for the given example:

import pandas as pd

salary = [['Google', 'Machine Learning Engineer', 121000],
          ['Google', 'Data Scientist', 109000],
          ['Google', 'Tech Lead', 129000],
          ['Facebook', 'Data Scientist', 103000]]

df = pd.DataFrame(salary)

This results in the following Dataframe:

print(df)

'''
          0                          1       2
0    Google  Machine Learning Engineer  121000
1    Google             Data Scientist  109000
2    Google                  Tech Lead  129000
3  Facebook             Data Scientist  103000
'''

An alternative is the pandas.DataFrame.from_records() method that generates the same output:

df = pd.DataFrame.from_records(salary)
print(df)
'''
          0                          1       2
0    Google  Machine Learning Engineer  121000
1    Google             Data Scientist  109000
2    Google                  Tech Lead  129000
3  Facebook             Data Scientist  103000
'''

If you want to add column names to make the output prettier, you can also pass those as a separate argument:

import pandas as pd

salary = [['Google', 'Machine Learning Engineer', 121000],
          ['Google', 'Data Scientist', 109000],
          ['Google', 'Tech Lead', 129000],
          ['Facebook', 'Data Scientist', 103000]]

df = pd.DataFrame(salary, columns=['Company', 'Job', 'Salary($)'])
print(df)

'''
    Company                        Job  Salary($)
0    Google  Machine Learning Engineer     121000
1    Google             Data Scientist     109000
2    Google                  Tech Lead     129000
3  Facebook             Data Scientist     103000
'''

If the first list of the list of lists contains the column name, use slicing to separate the first list from the other lists:

import pandas as pd

salary = [['Company', 'Job', 'Salary($)'],
          ['Google', 'Machine Learning Engineer', 121000],
          ['Google', 'Data Scientist', 109000],
          ['Google', 'Tech Lead', 129000],
          ['Facebook', 'Data Scientist', 103000]]

df = pd.DataFrame(salary[1:], columns=salary[0])
print(df)

'''
    Company                        Job  Salary($)
0    Google  Machine Learning Engineer     121000
1    Google             Data Scientist     109000
2    Google                  Tech Lead     129000
3  Facebook             Data Scientist     103000
'''

Slicing is a powerful Python feature and before you can master Pandas, you need to master slicing. To refresh your Python slicing skills, download my ebook “Coffee Break Python Slicing” for free.

Summary: To convert a list of lists into a Pandas DataFrame, use the pd.DataFrame() constructor and pass the list of lists as an argument. An optional columns argument can help you structure the output.

Related article: How to convert a list of lists into a Python DataFrame?

Convert List of Lists to List of Tuples

If you’re in a hurry, here’s the short answer: use the list comprehension statement [tuple(x) for x in list] to convert each element in your list to a tuple. This works also for list of lists with varying number of elements.

But there’s more to it and studying the two main method to achieve the same objective will make you a better coder. So keep reading:

Method 1: List Comprehension + tuple()

Problem: How to convert a list of lists into a list of tuples?

Example: You’ve got a list of lists [[1, 2], [3, 4], [5, 6]] and you want to convert it into a list of tuples [(1, 2), (3, 4), (5, 6)].

Solution: There are different solutions to convert a list of lists to a list of tuples. Use list comprehension in its most basic form:

lst = [[1, 2], [3, 4], [5, 6]]
tuples = [tuple(x) for x in lst]
print(tuples)
# [(1, 2), (3, 4), (5, 6)]

Try It Yourself:

This approach is simple and effective. List comprehension defines how to convert each value (x in the example) to a new list element. As each list element is a new tuple, you use the constructor tuple(x) to create a new tuple from the list x.

If you have three list elements per sublist, you can use the same approach with the conversion:

lst = [[1, 2, 1], [3, 4, 3], [5, 6, 5]]
tuples = [tuple(x) for x in lst]
print(tuples)
# [(1, 2, 1), (3, 4, 3), (5, 6, 5)]

You can see the execution flow in the following interactive visualization (just click the “Next” button to see what’s happening in the code):

And if you have a varying number of list elements per sublist, this approach still works beautifully:

lst = [[1], [2, 3, 4], [5, 6, 7, 8]]
tuples = [tuple(x) for x in lst]
print(tuples)
# [(1,), (2, 3, 4), (5, 6, 7, 8)]

You see that an approach with list comprehension is the best way to convert a list of lists to a list of tuples. But are there any alternatives?

Method 2: Map Function + tuple()

An alternative is to use the map function that applies a specified function on each element of an iterable. Guido van Rossum, the creator of Python, didn’t like the map() function as it’s less readable (and less efficient) than the list comprehension version (method 1 in this tutorial). You can read about a detailed discussion on how exactly he argued on my blog article.

So, without further ado, here’s how you can convert a list of lists into a list ot tuples using the map() function:

lst = [[1], [2, 3, 4], [5, 6, 7, 8]]
tuples = list(map(tuple, lst))
print(tuples)
# [(1,), (2, 3, 4), (5, 6, 7, 8)]

Try it yourself:

The first argument of the map() function is the tuple function name. This tuple() function converts each element on the given iterable lst (the second argument) into a tuple. The result of the map() function is an iterable so you need to convert it to a list before printing it to the shell because the default string representation of an iterable is not human-readable.

Related Articles

Convert List of Lists to CSV File

Problem: How to convert a list of lists to a csv file?

Example: Given is a list of list—for example salary data of employees in a given company:

salary = [['Alice', 'Data Scientist', 122000],
          ['Bob', 'Engineer', 77000],
          ['Ann', 'Manager', 119000]]

Your goal is to write the content of the list of lists into a comma-separated-values (CSV) file format. Your outfile should look like this:

# file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

Solution: There are four simple ways to convert a list of lists to a CSV file in Python.

  1. CSV: Import the csv module in Python, create a csv writer object, and write the list of lists to the file in using the writerows() method on the writer object.
  2. Pandas: Import the pandas library, create a Pandas DataFrame, and write the DataFrame to a file using the DataFrame method DataFrame.to_csv('file.csv').
  3. NumPy: Import the NumPy library, create a NumPy array, and write the output to a CSV file using the numpy.savetxt('file.csv', array, delimiter=',') method.
  4. Python: Use a pure Python implementation that doesn’t require any library by using the Python file I/O functionality.

My preference is method 2 (Pandas) because it’s simplest to use and most robust for different input types (numerical or textual).

Before we dive into these methods in more detail, feel free to play with them in our interactive code shell. Simply click the “Run” button and find the generated CSV files in the “Files” tab.

Method 1: Python’s CSV Module

You can convert a list of lists to a CSV file in Python easily—by using the csv library. This is the most customizable of all four methods.

salary = [['Alice', 'Data Scientist', 122000],
          ['Bob', 'Engineer', 77000],
          ['Ann', 'Manager', 119000]]

# Method 1
import csv
with open('file.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(salary)

Output:

# file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

In the code, you first open the file using Python’s standard open() command. Now, you can write content to the file object f.

Next, you pass this file object to the constructor of the CSV writer that implements some additional helper method—and effectively wraps the file object providing you with new CSV-specific functionality such as the writerows() method.

You now pass a list of lists to the writerows() method of the CSV writer that takes care of converting the list of lists to a CSV format.

You can customize the CSV writer in its constructor (e.g., by modifying the delimiter from a comma ',' to a whitespace ' ' character). Have a look at the specification to learn about advanced modifications.

Method 2: Pandas DataFrame to_csv()

List of lists to CSV

You can convert a list of lists to a Pandas DataFrame that provides you with powerful capabilities such as the to_csv() method. This is the easiest method and it allows you to avoid importing yet another library (I use Pandas in many Python projects anyways).

salary = [['Alice', 'Data Scientist', 122000],
          ['Bob', 'Engineer', 77000],
          ['Ann', 'Manager', 119000]]

# Method 2
import pandas as pd
df = pd.DataFrame(salary)
df.to_csv('file2.csv', index=False, header=False)

Output:

# file2.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

You create a Pandas DataFrame—which is Python’s default representation of tabular data. Think of it as an Excel spreadsheet within your code (with rows and columns).

The DataFrame is a very powerful data structure that allows you to perform various methods. One of those is the to_csv() method that allows you to write its contents into a CSV file.

You set the index and header arguments of the to_csv() method to False because Pandas, per default, adds integer row and column indices 0, 1, 2, …. Again, think of them as the row and column indices in your Excel spreadsheet. You don’t want them to appear in the CSV file so you set the arguments to False.

If you want to customize the CSV output, you’ve got a lot of special arguments to play with. Check out this article for a comprehensive list of all arguments.

Related article: Pandas Cheat Sheets to Pin to Your Wall

Method 3: NumPy savetext()

NumPy is at the core of Python’s data science and machine learning functionality. Even Pandas uses NumPy arrays to implement critical functionality.

You can convert a list of lists to a CSV file by using NumPy’s savetext() function and passing the NumPy array as an argument that arises from conversion of the list of lists.

This method is best if you have numerical data only—otherwise, it’ll lead to complicated data type conversions which are not recommended.

a = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

# Method 3
import numpy as np
a = np.array(a)
np.savetxt('file3.csv', a, delimiter=',')

Output:

# file3.csv
1.000000000000000000e+00,2.000000000000000000e+00,3.000000000000000000e+00
4.000000000000000000e+00,5.000000000000000000e+00,6.000000000000000000e+00
7.000000000000000000e+00,8.000000000000000000e+00,9.000000000000000000e+00

The output doesn’t look pretty: it stores the values as floats. But no worries, you can reformat the output using the format argument fmt of the savetxt() method (more here). However, I’d recommend you stick to method 2 (Pandas) to avoid unnecessary complexity in your code.

Related:

Method 4: Pure Python Without External Dependencies

If you don’t want to import any library and still convert a list of lists into a CSV file, you can use standard Python implementation as well: it’s not complicated and efficient. However, if possible you should rely on libraries that do the job for you.

This method is best if you won’t or cannot use external dependencies.

salary = [['Alice', 'Data Scientist', 122000],
          ['Bob', 'Engineer', 77000],
          ['Ann', 'Manager', 119000]]

# Method 4
with open('file4.csv','w') as f:
    for row in salary:
        for x in row:
            f.write(str(x) + ',')
        f.write('\n')

Output:

# file4.csv
Alice,Data Scientist,122000,
Bob,Engineer,77000,
Ann,Manager,119000,

In the code, you first open the file object f. Then you iterate over each row and each element in the row and write the element to the file—one by one. After each element, you place the comma to generate the CSV file format. After each row, you place the newline character '\n'.

Note: to get rid of the trailing comma, you can check if the element x is the last element in the row within the loop body and skip writing the comma if it is.

Related: Join the Finxter community and download your 8+ Python cheat sheets to refresh your code understanding.

Sort List of Lists by Key

Every computer scientist loves sorting things. In this section, I’ll show you how you can modify the default Python sorting behavior with the key argument.

Definition and Usage: To customize the default sorting behavior of the list.sort() and sorted() method, use the optional key argument by passing a function that returns a comparable value for each element in the list.

Related article: [Ultimate Guide] sort() function of Python lists

Syntax: You can call this method on each list object in Python (Python versions 2.x and 3.x). Here’s the syntax:

list.sort(key=None, reverse=False)

Arguments:

ArgumentDescription
key(Optional. Default None.) Pass a function that takes a single argument and returns a comparable value. The function is then applied to each element in the list. Then, the method sorts based on the key function results rather than the elements themselves.
reverse(Optional. Default False.) The order of the list elements. If False, sorting is in ascending order. If True, it’s in descending order.

Related articles:

The list.sort() method takes another function as an optional key argument that allows you to modify the default sorting behavior. The key function is then called on each list element and returns another value based on which the sorting is done. Hence, the key function takes one input argument (a list element) and returns one output value (a value that can be compared).

Here’s an example:

>>> lst = [[1, 2], [3, 2], [3, 3, 4], [1, 0], [0, 1], [4, 2]]
>>> lst.sort()
>>> lst
[[0, 1], [1, 0], [1, 2], [3, 2], [3, 3, 4], [4, 2]]
>>> lst.sort(key=lambda x:x[0])
>>> lst
[[0, 1], [1, 0], [1, 2], [3, 2], [3, 3, 4], [4, 2]]
>>> lst.sort(key=lambda x:x[1])
>>> lst
[[1, 0], [0, 1], [1, 2], [3, 2], [4, 2], [3, 3, 4]]

You can see that in the first two examples, the list is sorted according to the first inner list value.

In the third example, the list is sorted according to the second inner list value. You achieve this by defining a key function key=lambda x: x[1] that takes one list element x (a list by itself) as an argument and transforms it into a comparable value x[1] (the second list value).

Related article:

Sort List of Lists by First Element

Both the list sort() method and the sorted() built-in Python function sort a list of lists by their first element.

Here’s an example:

lst = [[1, 2, 3],
       [3, 2, 1],
       [2, 2, 2]]

lst.sort()
print(lst)
# [[1, 2, 3],
#  [2, 2, 2],
#  [3, 2, 1]]

The default sorting routine takes the first list element of any inner list as a decision criteria. Only if the first element would be the same for two values, the second list element would be taken as a tiebreaker.

Sort List of Lists Lexicographically

Problem: Given a list of lists. Sort the list of strings in lexicographical order! Lexicographical order is to sort by the first inner list element. If they are the same, you sort by the second inner list element, and so on.

Example:

[[1, 2, 3], [3, 2, 1], [2, 2, 2], [2, 0, 3]] --> 
[[1, 2, 3], [2, 0, 3], [2, 2, 2], [3, 2, 1]]

Solution: Use the list.sort() method without argument to solve the list in lexicographical order.

lst = [[1, 2, 3],
       [3, 2, 1],
       [2, 2, 2],
       [2, 0, 3]]

lst.sort()
print(lst)
'''
[[1, 2, 3],
 [2, 0, 3],
 [2, 2, 2],
 [3, 2, 1]]
'''

Sort List of Lists By Length

Problem: Given a list of lists. How can you sort them by length?

Example: You want to sort your list of lists [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]] by length—starting with the shortest list. Thus, your target result is [[4], [2, 2], [1, 2, 3], [1, 2, 3, 4]]. How to achieve that?

Solution: Use the len() function as key argument of the list.sort() method like this: list.sort(key=len). As the len() function is a Python built-in function, you don’t need to import or define anything else.

Here’s the code solution:

lst = [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]]

lst.sort(key=len)
print(lst)

The output is the list sorted by length of the string:

[[4], [2, 2], [1, 2, 3], [1, 2, 3, 4]]

You can also use this technique to sort a list of strings by length.

List Comprehension Python List of Lists

You’ll learn three ways how to apply list comprehension to a list of lists:

  • to flatten a list of lists
  • to create a list of lists
  • to iterate over a list of lists

Additionally, you’ll learn how to apply nested list comprehension. So let’s get started!

Python List Comprehension Flatten List of Lists

Problem: Given a list of lists. How to flatten the list of lists by getting rid of the inner lists—and keeping their elements?

Example: You want to transform a given list into a flat list like here:

lst = [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]]

# ... Flatten the list here ...

print(lst)
# [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]
Flatten a List of Lists with List Comprehension

Solution: Use a nested list comprehension statement [x for l in lst for x in l] to flatten the list.

lst = [[2, 2], [4], [1, 2, 3, 4], [1, 2, 3]]

# ... Flatten the list here ...
lst = [x for l in lst for x in l]

print(lst)
# [2, 2, 4, 1, 2, 3, 4, 1, 2, 3]

Explanation: In the nested list comprehension statement [x for l in lst for x in l], you first iterate over all lists in the list of lists (for l in lst). Then, you iterate over all elements in the current list (for x in l). This element, you just place in the outer list, unchanged, by using it in the “expression” part of the list comprehension statement [x for l in lst for x in l].

Try It Yourself: You can execute this code snippet yourself in our interactive Python shell. Just click “Run” and test the output of this code.

Can you flatten a three-dimensional list (= a list of lists of lists)? Try it in the shell!

Python List Comprehension Create List of Lists

Problem: How to create a list of lists by modifying each element of an original list of lists?

Example: You’re given the list

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

You want to add one to each element and create a new list of lists:

[[2, 3, 4], [5, 6, 7], [8, 9, 10]]

Solution: Use two nested list comprehension statements, one to create the outer list of lists, and one to create the inner lists.

lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new = [[x+1 for x in l] for l in lst]
print(new)
# [[2, 3, 4], [5, 6, 7], [8, 9, 10]]

Explanation: The main idea is to use as “expression” of the outer list comprehension statement a list comprehension statement by itself. Remember, you can create any object you want in the expression part of your list comprehension statement. Read more here.

Print List of Lists Without Brackets

Problem: Given a list of lists, print it one row per line—without brackets.

Example: Consider the following example list:

lst = [[1, 2, 3], [4, 5, 6]]

You want to print the list of lists with a newline character after each inner list:

1 2 3
4 5 6

Solution: Use a for loop and a simple print statement:

lst = [[1, 2, 3], [4, 5, 6]]

for x in lst:
    print(*x)

The output has the desired form:

1 2 3
4 5 6

Explanation: The asterisk operator “unpacks” all values in the inner list x into the print statement. You must know that the print statement also takes multiple inputs and prints them, whitespace-separated, to the shell.

Related articles:

Print List of Lists With Newline & Align Columns

Problem: How to print a list of lists with a new line after each list so that the columns are aligned?

Example: Say, you’re going to print the list of lists.

[['Alice', 'Data Scientist', 121000], 
 ['Bob', 'Java Dev', 99000], 
 ['Ann', 'Python Dev', 111000]]

How to align the columns?

Alice   'Data Scientist',  121000], 
 Bob    'Java Dev',           99000], 
 Ann    'Python Dev',      111000]]

Solution: Use the following code snippet to print the list of lists and align all columns (no matter how many characters each string in the list of lists occupies).

# Create the list of lists
lst = [['Alice', 'Data Scientist', '121000'],
       ['Bob', 'Java Dev', '99000'],
       ['Ann', 'Python Dev', '111000']]


# Find maximal length of all elements in list
n = max(len(x) for l in lst for x in l)

# Print the rows
for row in lst:
    print(''.join(x.ljust(n + 2) for x in row))

The output is the desired:

Alice           Data Scientist  121000          
Bob             Java Dev        99000           
Ann             Python Dev      111000   

Explanation:

  • First, you determine the length n (in characters) of the largest string in the list of lists using the statement max(len(x) for l in lst for x in l). The code uses a nested for loop in a generator expression to achieve this.
  • Second, you iterate over each list in the list of lists (called row).
  • Third, you create a string representation with columns aligned by ‘padding’ each row element so that it occupies n+2 characters of space. The missing characters are filled with empty spaces.

You can see the code in action in the following memory visualizer. Just click “Next” to see which objects are created in memory if you run the code in Python:

Related articles: You may need to refresh your understanding of the following Python features used in the code:

Python List of Lists Enumerate

Say, you’ve given the following code that uses the enumerate function on a list of lists:

lst = [['Alice', 'Data Scientist', '121000'],
       ['Bob', 'Java Dev', '99000'],
       ['Ann', 'Python Dev', '111000']]

for i,l in enumerate(lst):
    print('list ' + str(i) + ': ' + str(len(l)) + ' elements')

The output is:

list 0: 3 elements
list 1: 3 elements
list 2: 3 elements

The enumerate function creates an iterator of (index, element) pairs for all elements in a given list. If you have a list of lists, the list elements are list themselves. So, the enumerate function generates (index, list) pairs. You can use them in the loop body—for example, to print the length of the i-th list elements.

Remove Empty – Python List of Lists

How can you remove all empty lists from a list of lists? Say, you’ve got a list of lists [[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []] and you want all empty lists removed to obtain the list of lists [[1, 2, 3], [1, 2], [1, 2, 3, 4]].

Solution: Use list comprehension [x for x in list if x] to filter the list and remove all lists that are empty.

lst = [[1, 2, 3], [1, 2], [], [], [], [1, 2, 3, 4], [], []]
print([x for x in lst if x])
# [[1, 2, 3], [1, 2], [1, 2, 3, 4]]

The condition if x evaluates to False only if the list x is empty. In all other cases, it evaluates to True and the element is included in the new list.

Remove Duplicates – Python List of Lists