There are the following methods to convert a tuple to a list in Python.
- Method 1: Using the list() function
- Method 2: Using the list comprehension
- Method 3: Using the itertools.chain() function
- Method 4: Using the list.extend() function
- Method 5: Using an asterisk(*) operator
- Method 6: Using the for loop and append() method
Method 1: Using the list() function
To convert a tuple to a list in Python, you can use the list() method. The list() is a built-in method that 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 the list() method has converted a tuple into a list, and all the tuple elements are intact.
Method 2: Using list comprehension
To convert a list of tuples into a list, use list comprehension. The List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
# 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.
Method 3: Using the itertools.chain() method
You can also use the itertools.chain() method to convert a list of tuples 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]
Method 4: Using the list extend() method
You can also create an empty list and use the extend() method to add the elements of the tuple to the list, and that way, you can convert a tuple to a list.
main_tuple = (1, 2, 3)
main_list = []
main_list.extend(main_tuple)
print(main_list)
Output
[1, 2, 3]
Method 5: Using an asterisk(*) operator
Using the unpacking asterisk operator * from Python 3.5, use the expression [*t] creates a new list with the square bracket notation and unpacks all elements of the tuple t into the list.
def convert_tuple_to_list(tup):
return [*tup]
tupl = (1, 2, 8, 11)
print(convert_tuple_to_list(tupl))
tupl_2 = ('Vecna', 'Bob', 'Kali', 'Eleven')
print(convert_tuple_to_list(tupl_2))
tupl_3 = (11, )
print(convert_tuple_to_list(tupl_3))
Output
[1, 2, 8, 11]
['Vecna', 'Bob', 'Kali', 'Eleven']
[11]
The [*tup] is Python’s most concise one-liner to convert a tuple to a list.
Method 6: Using a for loop and append()
Create an empty list to fill in one tuple element at a time using the append() method and pass the tuple element into the function within a for loop body iterating over all tuple elements.
def convert_tuple_to_list(tup):
brand_new_list = []
for element in tup:
brand_new_list.append(element)
return brand_new_list
tupl = (1, 2, 8, 11)
print(convert_tuple_to_list(tupl))
tupl_2 = ('Vecna', 'Bob', 'Kali', 'Eleven')
print(convert_tuple_to_list(tupl_2))
tupl_3 = (11, )
print(convert_tuple_to_list(tupl_3))
Output
[1, 2, 8, 11]
['Vecna', 'Bob', 'Kali', 'Eleven']
[11]
The list append() method appends an element to the end of the list.
Conclusion
The easiest and most efficient way is to use Python’s list() method to convert a tuple to a list. The list() and tuple() return a new list and tuple when given an iterable object such as a list, tuple, set, range, etc.