Convert list of tuples to list?

Question:

How do I convert

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

to

[1, 2, 3]
Asked By: fanti

||

Answers:

Using simple list comprehension:

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

will give you:

[1, 2, 3]
Answered By: Levon

@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]
Answered By: Praveen Gollakota
>>> a = [(1,), (2,), (3,)]
>>> zip(*a)[0]
(1, 2, 3)

For a list:

>>> list(zip(*a)[0])
[1, 2, 3]
Answered By: fraxel

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)
Answered By: Andrew Clark

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]
Answered By: mstringer

There’s always a way to extract a list from another list by forin. In this case it would be:

[i[0] for i in e]

Answered By: Hopfield Sun

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]

Answered By: Samer Makary

One Liner yo!

list(*zip(*[(1,), (2,), (3,)]))
Answered By: MaK
>>> 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.

Answered By: Touhami

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

Answered By: SuperNova

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)
Answered By: Daniel Oscar

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]
Answered By: Prakash Dahal
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.