Possible Duplicate:
join list of lists in python
I have a list like this, where tuples are always of equal length:
[(1, 2), (4, 7), (6, 0)]
What's the most pythonic way to generate this?
[1, 2, 4, 7, 6, 0]
Possible Duplicate:
join list of lists in python
I have a list like this, where tuples are always of equal length:
[(1, 2), (4, 7), (6, 0)]
What's the most pythonic way to generate this?
[1, 2, 4, 7, 6, 0]
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))
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.
my_list = [(1, 2), (4, 7), (6, 0)]
print sum(my_list,())
result
(1, 2, 4, 7, 6, 0)