Flattening a nested list in Python involves converting a list with sublists into a single-level list. Here are two examples:
Example 1:
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]] flattened_list = [item for sublist in nested_list for item in sublist] print(flattened_list)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Example 2:
nested_list = [[1], [2, 3], [4, 5, 6], [7]] flattened_list = [item for sublist in nested_list for item in sublist] print(flattened_list)
Output:
[1, 2, 3, 4, 5, 6, 7]