5 Easy Ways to Convert a Tuple to a List in Python

Python provides various data structures to store the collection of data, such as “list” and “tuple”. The lists in Python are ordered and changeable (mutable), while tuples are ordered and unchangeable (immutable). While working with these data structures, a user may need to convert a tuple into a list for better memory management/efficiency. 

This tutorial will provide different methods to convert the tuple to a list using numerous examples.

Method 1: Using Inbuilt list() Function

The inbuilt “list()” function is used in the below code to convert the tuples into the lists:

Code:

tuple_value_1 = (45, 25, 50, 10)
tuple_value_2 = ('Joseph', 'Henry', 'Alex', 'Lily')
tuple_value_3 = (55, )
tuple_value_4 = 55,

list1= list(tuple_value_1)
print(list1)
print(type(list1), '\n')

list2= list(tuple_value_2)
print(list2)
print(type(list2), '\n')

list3= list(tuple_value_3)
print(list3)
print(type(list3), '\n')

list4= list(tuple_value_4)
print(list4)
print(type(list4), '\n')
  • The tuples named “tuple_value_1”, “tuple_value_2”, “tuple_value_3” and “tuple_value_4” are initialized in the program.
  • The “list()” function accepts the tuple as a parameter value and converts it into the list.

Output:

The given tuples have been converted into lists.

Method 2: Using List Comprehension

The “list comprehension” is used in the below code to convert the tuple to a list:

Code:

tuple_value = (45, 'Joseph', 50, 10)
list1 = [item for item in tuple_value]
print(list1)
print(type(list1))
  • The tuple named “tuple_value” is initialized in the program.
  • In the list comprehension, the for loop is used to iterate over the tuple and return the list.

Output:

The given tuple has been converted into the list.

The “tuple of tuples” or nested tuples can also be converted into a “list of lists” or nested lists using the list comprehension technique. The below example is utilized to convert the nested tuple to a nested list:

Code:

tuple_value = ((2,5,9),('A', 'B', 'C'), ('a', 34, 'b'))
list1 = [list(x) for x in tuple_value]
print(list1)
print(type(list1))
  • The “tuple of tuple” is initialized in the program.
  • Within the list comprehension, the “list()” function is used along with a for loop to convert the tuple into the list.

Output:

The nested tuple has been converted into a nested list.

Method 3: Using for Loop With append() Function

The traditional for loop is used along with the “append()” function to convert a tuple to a list in Python:

Code:

tuple_value = (45, 85, 98, 15)
list_value = []
for i in tuple_value:
    list_value.append(i)
print(list_value)
print(type(list_value))
  • The tuple named “tuple_value” is initialized in the program.
  • The empty list named “list_value” is initialized.
  • The for loop iterates over the tuple, and the “append()” function appends the tuple elements to the list.
  • The list_value is then printed on the screen using the “print()” function.

Output:

The given tuple named “tuple_value” has been converted into a list.

Method 4: Using Unpacking Python * Operator

The Python “*” unpacking operator is used to unpack the tuple iterable. The below code is utilized to convert the input tuple to a list:

Code:

tuple_value = (45, 85, 98, 15)
list_value = [*tuple_value]
print(list_value)
print(type(list_value))

The Python “*” unpacking operator is used with the input tuple and passed inside the square bracket to return the list.

Output:

The given tuple has been converted into the list using the * operator.

Method 5: Using enumerate() Function

The below code uses the “enumerate()” function to convert the tuple to a list:

Code:

tuple_value = (45, 85, 98, 15)
list_value = [x for i, x in enumerate(tuple_value)]
print(list_value)
print(type(list_value))
  • In the list comprehension, the “enumerate()” function takes the tuple as an argument and retrieves the enumerated object.
  • The for loop iterates through the enumerated object and retrieves the list object.

Output:

The input tuple has been converted into the list.

Conclusion

To convert a tuple to a list, the list() function, list comprehension, for loop with append() function, * operator, and enumerate() function is used in Python. A tuple can be converted into a list by passing it to the list() function. The list comprehension method is also simple and efficient for converting the tuple to a list in Python. Similarly, other methods, such as for loop with append() function, * operator, etc., are also used to convert the tuple to a list. This Python guide presented various methods to convert the tuple to a list using appropriate examples.