There are many ways in Python to flatten a list of lists.
First, you can use list comprehension.
Next, you can use itertools.chain. Here’s an example.
The itertools.chain function concatenates the sublists into a single iterable and then converts them to a list. Lastly, you can also use itertools.chain.from_iterable.
Here, itertools.chain.from_iterable achieves the same result as chain(*nested_list).
Ultimately, all methods will have a flattened list [1, 2, 3, 4, 5, 6, 7, 8, 9].
Want to know more about Python? Check out our blogs!