How to Convert a Tuple to a List and Back?
In this article, we will learn to convert a tuple to a list and a list to a tuple in Python. We will use some built-in functions and some custom code as well. Let's first have a quick look over what is a list and a tuple in Python.
Python List
Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values.
List Example:
list1 = ["Ram", "Arun", "Kiran"]
list2 = [16, 78, 32, 67]
list3 = ["apple", "mango", 16, "cherry", 3.4]
Python Tuple
Python has a built-in data type called a tuple. Data inside a tuple can be of any type say, integer, string or a float value, or even a tuple type. The tuple uses comma-separated values within round brackets or parentheses to store data. Tuples can be defined using any variable name and then assigning different values to the tuple inside the round brackets. The tuple is ordered, unchangeable, and allows duplicate values.
Tuple Example:
tuple1 = ("Ram", "Arun", "Kiran")
tuple2 = (16, 78, 32, 67)
tuple3 = ("apple", "mango", 16, "cherry", 3.4)
Different Ways to Convert a Tuple to a List
Let us look at two different ways of converting a tuple to a list. Round brackets of the tuple will change to square brackets of the list after conversion and elements will remain the same.
Example: Using list() Method
This example uses the built-in list()
method. It takes any sequence or iterable as an argument and converts it into a Python List. In this case, it takes a tuple of elements as arguments and converts it into a list.
input_tuple = (1,2,3,4)
to_list = list(input_tuple)
print("List: ",to_list)
List: [1, 2, 3, 4]
Example: Using the Unpacking Method
This approach uses Python's PEP8 Unpacking Method. The PEP proposes the use of *
iterable unpacking operator to convert a tuple to a list. We pass the tuple using *
as prefix inside the square bracket to convert to a list.
input_tuple = (1,2,3,4)
to_list = [*input_tuple]
print("List: ",to_list)
List: [1, 2, 3, 4]
Different Ways to Convert a List to a Tuple
Let us look at three different ways of converting a list to a tuple. Square brackets of the list will change to round brackets of tuple after conversion and elements will remain the same.
Example: Using tuple() Method
This example uses the built-in tuple()
method. It takes any sequence or iterable as an argument and converts it into a Python Tuple. In this case, it takes a list of elements as arguments and converts it into a tuple.
input_list = [1,2,3,4]
to_tuple = tuple(input_list)
print("Tuple: ",to_tuple)
Tuple: (1, 2, 3, 4)
Example: Using the Unpacking Method
This approach uses Python's PEP8 Unpacking Method. The PEP proposes the use of *
iterable unpacking operator to convert a list to a tuple. We pass the list using *
as prefix inside the round bracket to convert to a list. You have to pass a single comma with the list to avoid TypeError. This single comma essentially unpacks the list inside a tuple literal. This method is short, a bit faster but probably suffers from readability.
input_list = [1,2,3,4]
to_tuple = (*input_list,)
print("Tuple: ", to_tuple)
Tuple: (1, 2, 3, 4)
Example: Using For Loop
This is probably the smart way to perform the conversion. It is a small variation of tuple()
method as we use for loop with tuple().
input_list = [1,2,3,4]
to_tuple = tuple(i for i in input_list)
print("Tuple: ", to_tuple)
Tuple: (1, 2, 3, 4)
Conclusion
In this article, we learned three different way to convert a tuple to a list and vice-versa in Python by using several built-in functions such as list()
, tuple()
, and unpacking
method. We used some simple algorithms as well. Make sure not to use tuple and list keywords as identifiers in your program.