Before discussing how to convert a tuple to a list in python. Let’s first have a look at the quick introduction of tuple and a list in python.
List in Python
A list in python is a collection of ordered or sequential forms of elements that can be changed whenever we want and a list can have repeated elements as well. A list can be denoted by square braces.
Tuple in Python
A tuple in python is just a variable in which we can store multiple distinct values like a list. A tuple contains ordered elements and we can’t change them later because tuple values are not changeable. A tuple can be denoted by round braces().
How to Convert Tuple to List in Python
There are multiple ways to convert a tuple to a list in python we are going to discuss a few of them check out the ways below.
Tup = ('hello','world',False, 2,7) #tuple to list tup1 = list(Tup) #print list print(type(tup1)) print(tup1) #['hello', 'world', False, 2, 7]
We used the list method, which is a built-in method of python that converts a set or tuple into a list.
First, we declared a tuple and stored some distinct values in it then we used the list method, in the end, we just printed out the list. Check out the result below.
['hello', 'world', False, 2, 7]
As you can see from the output above the tuple has been converted to a list.
def tuptolist(t): return [*t] tup1 = (12, 21, 81, 111) print(tuptolist(tup1)) tup2 = ('Amazon','Ebay','Shopify','Alibaba') print(tuptolist(tup2)) tup3 = (11, False) print(tuptolist(tup3))
In the above code, we used the asterisk operator which will create a new list.
First, we defined a function and then we used the * operator in the return statement, then declared three tuples and printed out the list by calling the function(tuptolist).
By using the asterisk operator All elements of a tuple will be converted into the new list.
[12, 21, 81, 111] ['Amazon', 'Ebay', 'Shopify', 'Alibaba'] [11, False]
#first import itertools import itertools # declaring tuples list tuple = [(14, 226), ('hello', 'world'), (True, 61)] # applying itertools list1 = list(itertools.chain(*tuple)) # Printing output print(list1)
we imported the itertools and then created a tuple list after that we used the itertool.chain function which works like a loop and will print the tuple in a form of a list. Check out the result below.
[14, 226, 'hello', 'world', True, 61]
tup = ((15,16,18,False), ('Hi','Dear','Readers')) list1 = list(map(list,tup)) print(list1)
In the above code, we used the map function basically, a map function calculates the tuple elements and shows them in a list. We simply created a tuple then used the map function and then printed out the list. Check out the result below.
[[15, 16, 18, False], ['Hi', 'Dear', 'Readers']]
In this tutorial we talked about how to convert a tuple to a list in python, first, we discussed a short introduction to list and tuple then we saw the python list method with an example.
After that, we discussed the list method with an example then we saw the usage of an asterisk (*) operator with an example, and in the end we used the itertool.chian and map function as well for converting tuple to list in python.
That’s all for today hope you like the tutorial if you have any queries regarding this article feel free to contact us below. Thanks for reading.