Home »
Python
Python - Flatten a Nested List
Last Updated : May 01, 2025
In Python, flattening a nested list means converting a list of lists into a single list with all the elements. In this chapter, we will learn different ways to flatten nested lists using loops, list comprehensions, and built-in functions with the help of examples.
Flatten a Nested List Using a For Loop
You can flatten a nested list by iterating over each sublist and then over each item in the sublist.
Note: This method works for lists with one level of nesting.
Example
In this example, we are flattening a nested list of city names using a simple for loop.
# Creating a nested list of cities
nested_cities = [
["New York", "London"],
["Paris", "Tokyo"],
["Sydney", "Dubai"]]
# Flattening the list using a for loop
flat_list = []
for sublist in nested_cities:
for city in sublist:
flat_list.append(city)
# Displaying the flattened list
print(flat_list)
The output of the above code would be:
['New York', 'London', 'Paris', 'Tokyo', 'Sydney', 'Dubai']
Flatten a Nested List Using List Comprehension
List comprehension provides a concise way to flatten a nested list in a single line of code. This is suitable for lists with a single level of nesting.
Example
In this example, we are flattening the nested list of cities using list comprehension.
# Creating a nested list of cities
nested_cities = [
["New York", "London"],
["Paris", "Tokyo"],
["Sydney", "Dubai"]]
# Flattening the list using list comprehension
flat_list = [city for sublist in nested_cities for city in sublist]
# Displaying the flattened list
print(flat_list)
The output of the above code would be:
['New York', 'London', 'Paris', 'Tokyo', 'Sydney', 'Dubai']
Flatten a Nested List Using itertools.chain()
The itertools.chain()
function from the Python standard library can also be used to flatten a nested list.
Example
In this example, we are using itertools.chain()
to flatten the nested list of cities.
# Importing chain from itertools
from itertools import chain
# Creating a nested list of cities
nested_cities = [
["New York", "London"],
["Paris", "Tokyo"],
["Sydney", "Dubai"]]
# Flattening the list using itertools.chain
flat_list = list(chain.from_iterable(nested_cities))
# Displaying the flattened list
print(flat_list)
The output of the above code would be:
['New York', 'London', 'Paris', 'Tokyo', 'Sydney', 'Dubai']
Flatten a Nested List Using Recursion
For lists with multiple levels of nesting, you can use recursion to flatten them completely.
Example
In this example, we are using a recursive function to flatten a list that contains deeply nested city names.
# Defining a recursive function to
# flatten any level of nested list
def flatten_list(nested_list):
flat_list = []
for item in nested_list:
if isinstance(item, list):
flat_list.extend(flatten_list(item))
else:
flat_list.append(item)
return flat_list
# Creating a deeply nested list of cities
nested_cities = [["New York", ["London", ["Paris"]]], ["Tokyo", "Sydney"], "Dubai"]
# Flattening the list using recursion
flat_list = flatten_list(nested_cities)
# Displaying the flattened list
print(flat_list)
The output of the above code would be:
['New York', 'London', 'Paris', 'Tokyo', 'Sydney', 'Dubai']
Flatten a Nested List Using numpy
If your nested list contains numeric values, you can use the numpy
library to flatten it easily using numpy.flatten()
.
Example
In this example, we are flattening a nested list of population numbers using NumPy.
# Importing numpy
import numpy as np
# Creating a nested list of populations
nested_populations = [[8419600, 8982000], [2148000, 13929286], [5312163, 3331420]]
# Converting to numpy array and flattening
flat_array = np.array(nested_populations).flatten()
# Converting back to list
flat_list = flat_array.tolist()
# Displaying the flattened list
print(flat_list)
The output of the above code would be:
[8419600, 8982000, 2148000, 13929286, 5312163, 3331420]
Exercise
Select the correct option to complete each statement about flattening nested lists in Python.
- Given
nested = [[1, 2], [3, 4]]
, which method will flatten the list to [1, 2, 3, 4]
?
- What is the output of
list(itertools.chain.from_iterable([[1, 2], [3, 4]]))
?
- Which module provides the
chain.from_iterable()
method used for flattening?
Advertisement
Advertisement