How to Convert Python Tuple to List
Lists and tuples are Python’s most used data types. The tuple data type in Python is similar to a list with one difference that element values of a tuple can not be modified, and tuple items are put between parentheses instead of a square bracket. To convert the Python list to tuple, use the tuple() method.
Convert Python Tuple to List
To convert a tuple to list in Python, use the list() method. Python list() method takes a tuple as an argument and returns the list. The list() takes sequence types and converts them to lists.
Syntax
list(sequence)
Parameters
The sequence is a tuple that will be converted into the list.
Return Value
The list() method returns the list.
Example
tup = (21, 19, 11, 46, 18) print(tup) lt = list(tup) print(lt)
Output
(21, 19, 11, 46, 18) [21, 19, 11, 46, 18]
You can see that list() method has converted a tuple into a list and all the elements of the tuple are intact.
Convert a list of tuples into a list
To convert a list of tuples into a list, use the list comprehension.
# List of tuple initialization listoftuples = [("Apple", 1), ("Microsoft", 2), ("Amazon", 3)] # Using list comprehension out = [item for t in listoftuples for item in t] # Printing output print(out)
Output
['Apple', 1, 'Microsoft', 2, 'Amazon', 3]
Using list comprehension, we can convert a list of tuples into one single list.
You can also use itertools.chain() method to convert a list of tuple into a list.
# Importing itertools import itertools # List of tuple initialization tuple = [(11, 21), (31, 41), (51, 61)] # Using itertools out = list(itertools.chain(*tuple)) # Printing output print(out)
Output
[11, 21, 31, 41, 51, 61]
Conclusion
The list() and tuple() returns a new list and tuple when given an iterable object such as list, tuple, set, range, etc. That is it for this tutorial.