2

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]?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Aufwind
  • 25,310
  • 38
  • 109
  • 154
  • 1
    I found this one: `import itertools` and then `myCombinedList = itertools.chain(*mylistOfLists)` Helped me get going! – Aufwind Apr 27 '11 at 17:00

2 Answers2

2
myCombinedList = []
[myCombinedList.extend(inner) for inner in mylistOfLists]

Or:

import itertools
myCombinedIterable = itertools.chain.from_iterable(mylistOfLists)
myCombinedList = list(myCombinedIterable)
jathanism
  • 33,067
  • 9
  • 68
  • 86
0
res=[]    
for item in mylistOfList:
        res+=item
matcheek
  • 4,887
  • 9
  • 42
  • 73