Convert List to Tuple & Vice Versa in Python (Examples)
Hi! This tutorial will show you how to turn a list into a tuple and vice-versa in the Python programming language.
First, here is an overview of this tutorial:
Let’s jump into the Python code!
Create Sample List
Here, we will create the sample Python list that we will turn into a tuple (immutable) in this tutorial. Therefore, in your Python IDE, run the line of code below to create the sample list:
list_ = [1, 2, 3, 4]
Thereafter, we will demonstrate how to turn the tuple back into an identical list with list_.
Example 1: List to Tuple | Transform List into Tuple Using tuple() Function
In this first example, we will turn the list into a tuple using Python’s built-in tuple() function.
tuples_ = tuple(list_) print(tuples_) # (1, 2, 3, 4)
The tuple()
function literally converts the list into a tuple; and you can ascertain that the output is indeed a tuple object by running:
print(type(tuples_)) # <class 'tuple'>
As seen, our attempt was successful. Let’s check an alternative way next!
Example 2: List to Tuple | Transform List into Tuple Using `*` Unpacking Operator
In this second example, we will transform the list into a tuple using the unpacking operator *
.
tuples_ = (*list_,) print(tuples_) # (1, 2, 3, 4) print(type(tuples_)) # <class 'tuple'>
The unpacking operator *
unpacks the content of the list into a tuple.
Example 3: List to Tuple | Transform List into Tuple Using Tuple Comprehension
In this third and final example, we will use tuple comprehension to convert the list into a tuple.
tuples_ = tuple(x for x in list_) print(tuples_) # (1, 2, 3, 4) print(type(tuples_)) # <class 'tuple'>
The tuple comprehension above iterates through the items in the list and places those items inside a tuple.
Next, this tutorial will demonstrate how to reverse the process by converting the tuple back into the list using different methods.
Example 1: Tuple to List | Transform Tuple to List Using list() Function
In this first example, we will use Python’s built-in list() function to turn the tuple back into a list.
new_list = list(tuples_) print(new_list) # [1, 2, 3, 4]
You can confirm that the output is indeed a list by running:
print(type(new_list)) # <class 'list'>
It looks like it works!
Example 2: Tuple to List | Transform Tuple to List Using List Comprehension
In this second example, we will transform the tuple back into a list using list comprehension.
new_list = [i for i in tuples_] print(new_list) # [1, 2, 3, 4] print(type(new_list)) # <class 'list'>
The program above iterates through the tuple, and places each item in the tuple inside a list.
Example 3: Tuple to List | Transform Tuple to List Using For Loop
In this third and final example, we will write a for loop that will loop through the items in the tuple, and append them to a list.
new_list = [] for i in tuples_: new_list.append(i) print(new_list) # [1, 2, 3, 4] print(type(new_list)) # <class 'list'>
As seen, it is an easy method yet requires few more lines of code than the previous alternatives.
This tutorial has demonstrated how to convert a list into a tuple and vice-versa in the Python programming language. You can use any of the methods demonstrated above to convert a Python list to a tuple and vice-versa.
Video, Further Resources & Summary
Do you need more explanations on how to convert a list into a tuple and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain how to convert a list into a tuple and vice-versa in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these:
- Convert List to pandas DataFrame (3 Examples)
- Check if String Exists in List in Python (3 Examples)
- Access Dictionary within List in Python (Example)
- Count Duplicates in List in Python (2 Examples)
- Learn Python Programming
This post has shown how to convert a list into a tuple and vice-versa in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter