Flatten a Nested List Using List Comprehension in Python
In Python, you can flatten a nested list using list comprehension by iterating through each sublist and extracting its elements into a single-level list. This approach is both concise and efficient, making it an excellent choice for handling nested structures.
Examples
1. Flattening a Simple Nested List
Here, we have a nested list containing multiple sublists, and we use list comprehension to flatten it into a single list.
</>
Copy
# Nested list
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8]]
# Flattening using list comprehension
flattened_list = [item for sublist in nested_list for item in sublist]
# Printing the flattened list
print("Flattened List:", flattened_list)
Explanation:
nested_list
: A list containing sublists.for sublist in nested_list
: Iterates through each sublist.for item in sublist
: Extracts each element from the sublist.[item for sublist in nested_list for item in sublist]
: Generates a new flattened list.
Output:
Flattened List: [1, 2, 3, 4, 5, 6, 7, 8]
2. Flattening a List with Mixed Data Types
We can use the same method to flatten a nested list containing different data types.
</>
Copy
# Nested list with mixed data types
nested_list = [["apple", "banana"], [1, 2, 3], [True, False]]
# Flattening using list comprehension
flattened_list = [item for sublist in nested_list for item in sublist]
# Printing the flattened list
print("Flattened List:", flattened_list)
Explanation:
- The nested list contains strings, integers, and boolean values.
- List comprehension iterates through each sublist and extracts elements while maintaining data types.
Output:
Flattened List: ['apple', 'banana', 1, 2, 3, True, False]
3. Flattening a Deeply Nested List (One Level)
If we have a more deeply nested list where each sublist contains other sublists, this method will only flatten it one level.
</>
Copy
# Deeply nested list
nested_list = [[1, [2, 3]], [4, 5], [6, [7, 8]]]
# Flattening using list comprehension (one level)
flattened_list = [item for sublist in nested_list for item in (sublist if isinstance(sublist, list) else [sublist])]
# Printing the flattened list
print("Flattened List:", flattened_list)
Explanation:
- The nested list contains elements where some sublists have another level of nesting.
- The
isinstance(sublist, list)
check ensures we handle non-list elements correctly. - This approach flattens only one level, so deeper nested lists remain unflattened.
Output:
Flattened List: [1, [2, 3], 4, 5, 6, [7, 8]]
4. Flattening a List of Tuples
List comprehension can also flatten a nested list containing tuples.
</>
Copy
# Nested list with tuples
nested_list = [(1, 2), (3, 4), (5, 6)]
# Flattening using list comprehension
flattened_list = [item for sublist in nested_list for item in sublist]
# Printing the flattened list
print("Flattened List:", flattened_list)
Explanation:
- Each tuple is treated as a sublist.
- List comprehension iterates through each tuple and extracts elements.
Output:
Flattened List: [1, 2, 3, 4, 5, 6]
Conclusion
Depending on the structure of your nested list, you can:
- Use
[item for sublist in nested_list for item in sublist]
for simple nested lists. - Handle different data types, including strings, numbers, and boolean values.
- Flatten a list of tuples similarly.
- Use additional conditions to partially flatten deeply nested lists.