Python: Combining a list of lists

Question:

Suppose I have a list of lists, like [[1, 2], [3, 4], [5, 6], [7, 8]]. What is the most elegant way in python to get [1, 2, 3, 4, 5, 6, 7, 8]?

Asked By: Aufwind

||

Answers:

res=[]    
for item in mylistOfList:
        res+=item
Answered By: matcheek
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]

Or:

import itertools
myCombinedIterable = itertools.chain.from_iterable(mylistOfLists)
myCombinedList = list(myCombinedIterable)
Answered By: jathanism
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.