
Nested list comprehension in Python (explained simply)
Running list comprehension on a nested list will create a new list by performing an operation on each nested element. In this post, I will explain how to run nested list comprehension and cover some common questions that users have.
Running list comprehension on a nested list will create a new list by performing an operation on each nested element. In this post, I will explain how to run nested list comprehension and cover some common questions that users have.
What is nested list comprehension in Python?
List comprehension is a method in Python which creates a new list by running an operation on an existing list. Nested list comprehension is when this process is run on a list of lists where each nested list and element is operated upon. The common alternative to using nested list comprehension is a for loop, which is often slower and requires more code.
Simple example of list comprehension syntax
Here is an example of the syntax for standard list comprehension:
list_ = [1,2,3,4]
new_list = [x * 2 for x in list_ if x%2 == 0]
"""
Output:
[4, 8]
"""
"""
The verbal equivelant of this syntax would be:
["Do this operation" for "These values" if "This condition is met"]
"""
Simple example of nested list comprehension syntax
Developing upon the standard example, this is the syntax for nested list comprehension:
nested_list = [[1,2],[3,4]]
new_list = [[x * 2 for x in sublist] for sublist in nested_list]
"""
Output:
[[2, 4], [6, 8]]
"""
Alternatives to nested list comprehension in Python
When creating a new list from an existing one, there are several alternatives available. Let’s compare the two most common alternatives: for loop and lambda functions.
What is the difference between list comprehension and for loop?
The main difference between list comprehension and a for loop is speed. Generally speaking, list comprehension is faster than a for loop when running operations to create a new list. Another minor difference is that list comprehension results in cleaner code as it takes up fewer lines than a for loop.
Is list comprehension faster than for loop?
Yes, list comprehension is often faster than a for loop when creating a new list.
What is the difference between lambda function and list comprehension?
The key difference between list comprehension and lambda functions is that list comprehension creates a new list, whilst a lambda function can result in either a new list or a value. Another difference is speed. In most typical use cases, list comprehension is faster at creating new lists than a lambda function.
Is lambda faster than list comprehension?
Both lambda functions and list comprehension are efficient methods of creating a new list in Python, but generally list comprehension is the faster method for creating a new list.
Nested list comprehension example in Python
Let’s suppose that you have a nested list in Python and you would like to run list comprehension on each element of the nested list.
Here are two examples of how this could be executed, either resulting in a single concatenated list or keeping the original nested structure.
List comprehension on nested list and concatenate to one list example
This example will run an operation on each element of the nested lists and create the new list as one single concatenated list.
nested_list = [[1,2,3],[4,5,6],[7,8,9]]
new_list = [x**2 for sublist in nested_list for x in sublist]
"""
Output:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
"""
# Adding a condition to the list comprehension
new_list = [x**2 for sublist in nested_list for x in sublist if x % 2 == 0]
"""
Output:
[4, 16, 36, 64]
"""
List comprehension on nested list and keep original list structure example
This example will run an operation on each element of the nested list and create a new list with the existing nested structure.
nested_list = [[1,2,3],[4,5,6],[7,8,9]]
new_list = [[x**2 for x in sublist] for sublist in nested_list]
"""
Output:
[[1, 4, 9], [16, 25, 36], [49, 64, 81]]
"""
# Adding a condition to the list comprehension
new_list = [[x**2 for x in sublist if x % 2 == 0] for sublist in nested_list]
"""
Output:
[[4], [16, 36], [64]]
"""
Related articles
Create a YAML config file
Pandas groupby aggregate functions