Python: Combining a list of lists [duplicate]
-
24-10-2019 - |
Question
Possible Duplicate:
Flattening a shallow list in Python
Let's assume I have a list of lists.
mylistOfLists = [[1, 2], [3, 4], [5, 6], [7, 8]]
What is the most elegant way in python to get this result?
myCombinedList = [1, 2, 3, 4, 5, 6]
Thank you for any suggestions!
Solution
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]
Or:
import itertools
myCombinedIterable = itertools.chain.from_iterable(mylistOfLists)
myCombinedList = list(myCombinedIterable)
OTHER TIPS
res=[]
for item in mylistOfList:
res+=item
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow