I have a list like [(1, 2), (4, 7), (6, 0)]
, where tuples are always of equal length. What's the most pythonic way to generate [1, 2, 4, 7, 6, 0]
?
Asked
Active
Viewed 3,822 times
0

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

Eric
- 95,302
- 53
- 242
- 374
-
2How can someone with 10k+ rep ask this question, which has been answered 101 times on SO already? – wim Dec 12 '11 at 22:52
-
1@wim Because there is on his user's page : _"Languages: •Java •C •C++ •HTML •CSS •PHP •Javascript"_ -> not Python – eyquem Dec 13 '11 at 11:33
-
@eyquem experienced users of Stack Overflow are supposed to understand *how to use the site*, which includes [researching before asking a question](https://meta.stackoverflow.com/questions/261592), which includes checking for duplicates. I'm pretty sure that was also true in 2011. – Karl Knechtel Sep 06 '22 at 06:03
3 Answers
10
You can use the list comprehension :
my_list = [(1, 2), (4, 7), (6, 0)]
result = [x for t in my_list for x in t]
or
result = list(itertools.chain.from_iterable(my_list))

Stan
- 8,710
- 2
- 29
- 31

Sven Marnach
- 574,206
- 118
- 941
- 841
-
In fact, my "tuples" are generated from objects. Is there a better way to write `[x for o in myObjects for x in (o.a, o.b)]`? – Eric Dec 12 '11 at 22:21
-
@Eric: One option would be to write a function `flatten()` (with one of the implementations from my answer) and use `flatten((o.a, o.b) for o in my_objects)`. You'll have to choose yourself what you like most. – Sven Marnach Dec 12 '11 at 22:25
-
-
1@Eric: Of course you could. You could add an `__iter__()` to the class of the objects, implemented as `yield self.a; yield self.b`. Or you could use `final_list = []; for o in my_objects: final_list.append(o.a); final_list.append(o.b)`. There are countless options, and it really doesn't matter too much which of them you choose. – Sven Marnach Dec 12 '11 at 22:31
-
0
If you're not using Python 3,
reduce(lambda x,y: x+y, sequence)
also works. Mileage may vary on how pythonic it is since reduce() has been removed, but alternative solutions are always nice.

Robert Mastragostino
- 705
- 1
- 8
- 13
-
2
-
1This has O(n^2) complexity (and is just a cryptic way of writing `sum(sequence, ())`, which also has quadratic complexity). Note that `reduce()` isn't gone in Python 3 -- it's just moved to `functools`. – Sven Marnach Dec 12 '11 at 22:33
0
my_list = [(1, 2), (4, 7), (6, 0)]
print sum(my_list,())
result
(1, 2, 4, 7, 6, 0)

eyquem
- 26,771
- 7
- 38
- 46
-
-
@Sven Marnach OK. In fact, I have very little understanding of these O(1) , O(2) etc considerations. How do you know it is quadratic please ? – eyquem Dec 13 '11 at 14:07
-
In every step, the whole tuple created so far needs to be copied to a new tuple with two additional items. If you do this with a long list, towards the end of the operation every single addition of two items is quite expensive. You can also see this by measuring the time it takes. [Using a list of ten times the length result in hundred times the run time](https://gist.github.com/1472334). – Sven Marnach Dec 13 '11 at 14:38
-
@Sven Marnach I was thinking that an underlying process in C could read the values in tuples like an iterator and make the creation of the final tuple only when all its elements would have been cumulated without making intermediate tuples of greater and greater length. Thank you. – eyquem Dec 13 '11 at 14:58