Pythonic way to explode a list of tuples

I need to do the opposite of this

Multiple tuples for a bipartisan tuple in Python?

Namely, I have a list of tuples

[(1,2), (3,4), (5,6)]

and you need to create this

[1,2,3,4,5,6]

I personally would do it

>>> tot = []
>>> for i in [(1,2), (3,4), (5,6)]:
...     tot.extend(list(i))

but I would like to see something more pleasant.

+5
source share
3 answers

The most effective way to do this is:

tuples = [(1,2), (3,4), (5,6)]
[item for t in tuples for item in t]

Exit

[1, 2, 3, 4, 5, 6]

Here is a comparison that I made in different ways to do this on a duplicate question.

I know that someone is going to offer this solution.

sum(tuples, ())

, ! . Alex

: , .

+18
>>> import itertools
>>> tp = [(1,2),(3,4),(5,6)]
>>> lst = list(itertools.chain(*tp))
>>> lst
[1, 2, 3, 4, 5, 6]

, , , list().

+6
l = [(1,2), (3,4), (5,6)]
reduce (lambda x,y: x+list(y), l, [])
+2
source

All Articles