Lists and tuples are Python’s most used data types. The tuple data type in Python is similar to a list with one different element value of a tuple that can’t be modified, and tuple items are put between parentheses instead of a square bracket.
Python tuple to list
To convert a tuple to list in Python, use the list() method. The list() is a built-in Python 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.
Convert a list of tuples into a list
To convert a list of tuples into a list, use list comprehension. 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.
You can also use 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]
Converting tuple to list using 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.
# app.py 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.
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.
# app.py 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 the most concise one-liner to convert a tuple to a list in Python.
Conclusion
Use the list() method in Python 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. To convert the Python list to tuple, use the tuple() method.
That is it for this tutorial.