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
Active
Viewed 343 times
2

Karl Knechtel
- 62,466
- 11
- 102
- 153

Aufwind
- 25,310
- 38
- 109
- 154
-
1I found this one: `import itertools` and then `myCombinedList = itertools.chain(*mylistOfLists)` Helped me get going! – Aufwind Apr 27 '11 at 17:00
2 Answers
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
-
If you want to extend the list, yes. (You can't extend something that's not already there). – jathanism Apr 27 '11 at 16:55