How do I flatten a nested list in Python?

Flattening a nested list in Python refers to the process of converting a list that contains other lists (or nested lists) into a single list with all the elements. This can be accomplished using various methods such as list comprehensions, the `itertools.chain()` function, or recursion.

Flatten nested list, Python list comprehension, itertools, recursive flattening
This article explains how to flatten a nested list in Python using different techniques.
# Example using list comprehension def flatten(nested_list): return [item for sublist in nested_list for item in sublist] nested_list = [[1, 2, 3], [4, 5], [6]] flat_list = flatten(nested_list) print(flat_list) # Output: [1, 2, 3, 4, 5, 6]

Flatten nested list Python list comprehension itertools recursive flattening