fanti
fanti

Reputation: 1927

Convert list of tuples to list?

How do I convert

[(1,), (2,), (3,)]

to

[1, 2, 3]

Upvotes: 75

Views: 72793

Answers (12)

Prakash Dahal
Prakash Dahal

Reputation: 4875

If it is already a numpy array, use ravel() method which is more faster than list comprehension.

If it is already a list, list comprehension is better.

Most of the answers above only prints the first element not all the elements

For numpy arrays

#arr = np.array([(1,2), (2,3), (3,4)])

#faster than list comprehension
arr.ravel().tolist()

#output => [1,2,2,3,3,4]

For list

list_ = [(1,2), (2,3), (3,4)]

[x for y in list_ for x in y]

#output => [1,2,2,3,3,4]

Upvotes: 1

SuperNova
SuperNova

Reputation: 27486

Using operator or sum

>>> from functools import reduce ### If python 3
>>> import operator
>>> a = [(1,), (2,), (3,)]
>>> list(reduce(operator.concat, a))
[1, 2, 3]

(OR)

>>> list(sum(a,()))
[1, 2, 3]
>>> 

If in python > 3 please do the import of reduce from functools like from functools import reduce

https://docs.python.org/3/library/functools.html#functools.reduce

Upvotes: 4

Touhami
Touhami

Reputation: 809

>>> a = [(1,), (2,), (3,)]
>>> b = map(lambda x: x[0], a)
>>> b
[1, 2, 3]

With python3, you have to put the list(..) function to the output of map(..), i.e.

b = list(map(lambda x: x[0], a))

This is the best solution in one line using python built-in functions.

Upvotes: 7

Daniel Oscar
Daniel Oscar

Reputation: 297

In these situations I like to do:

a = [(1,), (2,), (3,)]
new_a = [element for tup in a for element in tup]

This works even if your tuples have more than one element. This is equivalent to doing this:

a = [(1,), (2,), (3,)]
new_a = []
for tup in a:
    for element in tup:
        new_a.append(element)

Upvotes: 0

MaK
MaK

Reputation: 1728

One Liner yo!

list(*zip(*[(1,), (2,), (3,)]))

Upvotes: 2

Samer Makary
Samer Makary

Reputation: 1915

You can also use sum function as follows:

e = [(1,), (2,), (3,)] 
e_list = list(sum(e, ()))

And it also works with list of lists to convert it into a single list, but you will need to use it as follow:

e = [[1, 2], [3, 4], [5, 6]]
e_list = list(sum(e, []))

This will give you [1, 2, 3, 4, 5, 6]

Upvotes: 6

Hopfield Sun
Hopfield Sun

Reputation: 61

There's always a way to extract a list from another list by ...for...in.... In this case it would be:

[i[0] for i in e]

Upvotes: 4

mstringer
mstringer

Reputation: 2302

You can also unpack the tuple in the list comprehension:

e = [(1,), (2,), (3,)]
[i for (i,) in e]

will still give:

[1, 2, 3]

Upvotes: 3

Praveen Gollakota
Praveen Gollakota

Reputation: 39010

@Levon's solution works perfectly for your case.

As a side note, if you have variable number of elements in the tuples, you can also use chain from itertools.

>>> a = [(1, ), (2, 3), (4, 5, 6)]
>>> from itertools import chain
>>> list(chain(a))
[(1,), (2, 3), (4, 5, 6)]
>>> list(chain(*a))
[1, 2, 3, 4, 5, 6]
>>> list(chain.from_iterable(a)) # More efficient version than unpacking
[1, 2, 3, 4, 5, 6]

Upvotes: 73

Andrew Clark
Andrew Clark

Reputation: 208705

Here is another alternative if you can have a variable number of elements in the tuples:

>>> a = [(1,), (2, 3), (4, 5, 6)]
>>> [x for t in a for x in t]
[1, 2, 3, 4, 5, 6]

This is basically just a shortened form of the following loops:

result = []
for t in a:
    for x in t:
        result.append(x)

Upvotes: 38

Levon
Levon

Reputation: 143162

Using simple list comprehension:

e = [(1,), (2,), (3,)]
[i[0] for i in e]

will give you:

[1, 2, 3]

Upvotes: 103

fraxel
fraxel

Reputation: 35319

>>> a = [(1,), (2,), (3,)]
>>> zip(*a)[0]
(1, 2, 3)

For a list:

>>> list(zip(*a)[0])
[1, 2, 3]

Upvotes: 7

Related Questions