Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert list of tuples to list?

Tags:

python

How do I convert

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

to

[1, 2, 3] 
like image 612
fanti Avatar asked Jun 07 '12 23:06

fanti


People also ask

How do I make a list from a list of tuples?

List comprehension along with zip() function is used to convert the tuples to list and create a list of tuples. Python iter() function is used to iterate an element of an object at a time. The 'number' would specify the number of elements to be clubbed into a single tuple to form a list.

Can you turn tuples into lists?

We can use the list() function to convert tuple to list in Python. After writing the above code, Ones you will print ” my_tuple ” then the output will appear as a “ [10, 20, 30, 40, 50] ”. Here, the list() function will convert the tuple to the list.

How do I convert a tuple to a list in Python?

To convert a tuple into list in Python, call list() builtin function and pass the tuple as argument to the function. list() returns a new list generated from the items of the given tuple.

How do you convert a nested tuple to a list?

To convert a tuple of tuples to a list of lists: Pass the list() class and the tuple of tuples to the map() function. The map() function will pass each nested tuple to the list() class. Pass the map object to the list class to convert it to a list.


1 Answers

Using simple list comprehension:

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

will give you:

[1, 2, 3] 
like image 136
Levon Avatar answered Oct 14 '22 05:10

Levon