Here are 3 ways to convert a tuple to a list in Python:
(1) Using a list() function:
my_tuple = ("item_1", "item_2", "item_3", ...)
my_list = list(my_tuple)
(2) Using a list comprehension:
my_tuple = ("item_1", "item_2", "item_3", ...)
my_list = [i for i in my_tuple]
(3) Using a for loop:
my_tuple = ("item_1", "item_2", "item_3", ...)
my_list = []
for i in my_tuple:
my_list.append(i)
Examples of Converting a Tuple to a List in Python
Example 1: Convert a tuple to a list using a list() function
First, create a tuple:
my_tuple = ("a", "mm", "ppp", "bb", "cc")
print(my_tuple)
print(type(my_tuple))
Note that the last print of “print(type(my_tuple))” was added to indicate that you actually created a tuple:
('a', 'mm', 'ppp', 'bb', 'cc')
<class 'tuple'>
Next, convert the tuple to a list using a list() function:
my_tuple = ("a", "mm", "ppp", "bb", "cc")
my_list = list(my_tuple)
print(my_list)
print(type(my_list))
As you can see, the tuple was indeed converted to a list:
['a', 'mm', 'ppp', 'bb', 'cc']
<class 'list'>
Example 2: Convert a tuple to a list using a list comprehension
To convert the same tuple (from the first example) to a list using a list comprehension:
my_tuple = ("a", "mm", "ppp", "bb", "cc")
my_list = [i for i in my_tuple]
print(my_list)
print(type(my_list))
You’ll get the same list:
['a', 'mm', 'ppp', 'bb', 'cc']
<class 'list'>
Example 3: Convert a tuple to a list using a for loop
Finally, convert the tuple to a list using a for loop:
my_tuple = ("a", "mm", "ppp", "bb", "cc")
my_list = []
for i in my_tuple:
my_list.append(i)
print(my_list)
print(type(my_list))
The result is the same list:
['a', 'mm', 'ppp', 'bb', 'cc']
<class 'list'>
The following guide explains how to convert a list to a tuple.