1

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!

Community
  • 1
  • 1
Aufwind
  • 23,814
  • 37
  • 104
  • 151
  • 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
  • 31,919
  • 9
  • 67
  • 86
0
res=[]    
for item in mylistOfList:
        res+=item
matcheek
  • 4,539
  • 9
  • 40
  • 69