In this article, we are going to learn How to convert tuple to list in Python we need the data type conversions like changing the form of data from one type to another. Most of the time this kind of requirement we get when we need to use a specific function that is supported by a new desired data type and not with the existing one. In this kind of situation, we quickly change the data to that data type and use the function that we need.
Here in this article, we are trying to cover, a similar use case where we will be learning how we can convert data that is in the form of a tuple to a list representation. So let us begin with this tutorial and learn the technique that we can use to get the job done.
So here is the first technique that we will learn:
1. List() constructor to convert tuples to list
The first example, we are going to use is by making use of the list() function. We will use a tuple and then pass this tuple to the list() function so that the list function can return us the data in the form of a list.
Once we are done with calling the list() function on this tuple data, then we will verify the type of the returned object. We can simply call the type() function to confirm the type of data is changed to a list now.
Below is the code sample of this approach which shows how we are doing this.
#Program to convert a tuple to a Python list tupleint= ('C#','PYTHON','JAVA',2,3,4,5,6) #converting tuple to list tuple_to_list = list(tupleint) #checking the datatype print('data type=',type(tuple_to_list)) print('tuple to list=\n',tuple_to_list)
From the output, we can see that now the type is showing as a list class and the data is also represented in the form of a list instead of the original type which was a tuple.
data type= <class 'list'> tuple to list= ['C#', 'PYTHON', 'JAVA', 2, 3, 4, 5, 6]
2. Convert tuples of tuples to list
The second example which we are going to see is trying to convert the tuples of tuples to a list. In this example, we are going to take a tuple, which includes its elements as tuples. Let us understand with an example of how we will Convert tuples of tuples to the list.
#Program to convert a tuple to a Python list tupleoftuple= (('C#',1),('PYTHON',2),('JAVA',3),(2,3),(4,5,6)) #converting list of tuples list_of_tuples = list(tupleoftuple) #checking the datatype print('data type=',type(tupleoftuple)) print('list of nested tuples =\n',list_of_tuples)
data type= <class 'tuple'> list of nested tuples = [('C#', 1), ('PYTHON', 2), ('JAVA', 3), (2, 3), (4, 5, 6)]
I hope you learnt with the examples and will be able to use the technique in your code.
Conclusion: We hope you are feeling confident now after learning how we can convert the tuple to a list. In Data Science and Data analysis problem we make use of the data conversion very often because sometime we do not get the data in the form we wish it. These type of conversion techniques helps us a lot and we can convert our data on the fly.
Hope this helped you learning and making your understanding!