Today I Learned
Search…
Easy & Lazy way to flatten a python list
Posted on 13 Nov, 2021
1
from itertools import chain
2
myList = [["bhupesh", 1], ["amanda", 2], ["robin", 3]]
3
​
4
print(list(chain.from_iterable(myList)))
5
# ['bhupesh', 1, 'amanda', 2, 'robin', 3]
Copied!
itertools.chain.from_iterable would work lazily, i.e no work done unless you loop over the returned iterator
Copy link
Edit on GitHub