Sometimes, we want to join list of lists in Python.
In this article, we’ll look at how to join list of lists in Python.
How to join list of lists in Python?
To join list of lists in Python, we can use the itertools.chain.from-_iterable
method.
For instance, we write
import itertools
a = [['a','b'], ['c']]
print(list(itertools.chain.from_iterable(a)))
to call itertools.chain.from_iterable
with a
to combine the lists in the list a
into one flattened iterator and return it.
Then we call list
to convert the iterator into a list.
Conclusion
To join list of lists in Python, we can use the itertools.chain.from-_iterable
method.