- How to convert a list into a tuple in Python?
- List
- Tuple
- Using the tuple() built-in function
- Example 1
- Output
- Example 2
- Output
- Using a loop
- Example
- Output
- Using unpack list inside the parenthesis(*list, )
- Output
- 3 Ways to Convert List to Tuple in Python
- What is a List in Python?
- What is Tuple in Python?
- Output
- Difference between List and Tuple
- Convert List to Tuple in Python
- 1) Using tuple() builtin function
- 2) Using loop inside the tuple
- 3) Unpack list inside the parenthesis
- Conclusion
- Convert List to Tuple in Python
- How To Convert A List To A Tuple In Python?
- Method — 1 : Using tuple builtin function
- Method — 2: Using loop inside the tuple
- Method — 3 : Using list
- Conclusion
- Learn More
How to convert a list into a tuple in Python?
List and Tuple in Python are the class of data structure. The list is dynamic, whereas the tuple has static characteristics. We will understand list and tuple individually first and then understand how to convert a list into a tuple.
List
Lists are a popular and widely used data structures provided by Python. A List is a data structure in python that is mutable and has an ordered sequence of elements. Following is a list of integer values −
If you execute the above snippet, it produces the following output −
Tuple
A tuple is a collection of python objects that are separated by commas which are ordered and immutable. Immutable implies that the tuple objects once created cannot be altered. Tuples are sequences, just like lists. The differences between tuples and lists are, that tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
tup=('tutorials', 'point', 2022,True) print(tup)
If you execute the above snippet, produces the following output −
('tutorials', 'point', 2022, True)
In this article, we convert a list into a tuple by using a few methods. Each of these methods are discussed below.
Using the tuple() built-in function
An iterable can be passed as an input to the tuple () function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.
Example 1
In the following code, we convert a list to a tuple using the tuple() built-in function.
list_names=['Meredith', 'Kristen', 'Wright', 'Franklin'] tuple_names= tuple(list_names) print(tuple_names) print(type(tuple_names))
Output
(‘Meredith’, ‘Kristen’, ‘Wright’, ‘Franklin’)
Example 2
Following is an another example of this −
my_list = [1, 2, 3] my_tuple = tuple(my_list) print(my_tuple)
Output
This will give the output −
Using a loop
This method is similar to the above one, but in this case, we do not send the complete list as an argument whereas we retrieve each element from the list using a for loop to send it as an argument for the tuple() built-in function.
Example
The following is an example code to convert a list to a tuple using a loop.
list_names=['Meredith', 'Kristen', 'Wright', 'Franklin'] tuple_names= tuple(i for i in list_names) print(tuple_names) print(type(tuple_names))
Output
(‘Meredith’, ‘Kristen’, ‘Wright’, ‘Franklin’)
Using unpack list inside the parenthesis(*list, )
This method is faster when compared to others, but it is not efficient and difficult to understand. Inside the parenthesis, you can unpack the list elements. The list just unpacks the elements within the tuple literal, which is generated by a single comma (,).
In this example code, we use the unpacking method to convert a list to a tuple.
list_names=['Meredith', 'Kristen', 'Wright', 'Franklin'] tuple_names= (*list_names,) print(tuple_names) print(type(tuple_names))
Output
(‘Meredith’, ‘Kristen’, ‘Wright’, ‘Franklin’)
3 Ways to Convert List to Tuple in Python
There may be times when you wish to have a specific type of a variable during programming. Typecasting is a process used to change the variables declared in a specific data type into a different data type to match the operation performed by the code snippet. Python is an object-oriented language that consists of many data types. Today, we will learn List and Tuple data type in python and some methods to convert python list to a tuple in detail.
What is a List in Python?
Python lists are a data structure similar to dynamically sized arrays. The most important feature of python’s list is that it need not be homogeneous always. A list can contain elements like integers, strings, and also objects. Therefore, a python list is an ordered and changeable collection of data objects, unlike an array containing a single type of data.
The list is the most versatile and frequently used data structure in python. You can create a list by placing all the elements inside the square brackets([]) separated by commas. All these elements can be further accessed by the index number inside the square brackets, just like arrays. Remember that in the list data structure, indexing begins from zero(0), which means the first element of the list will be accessed with 0 inside the square brackets. Also, the list is mutable. That means you can add, update or remove elements of the list after it has been created. Below example shows the python list in detail:
For Example
list1 = ["favtutor", 1, 2.30] print(list1)
What is Tuple in Python?
Tuples in Python are a data structure used to store multiple elements in a single variable. Just like list data structure, a tuple is homogenous. Therefore, a tuple can consist of elements of multiple data types at the same time. You can create a tuple by placing all elements inside the parentheses(()) separated by commas. Note that the parentheses are an option, however, it is always a good practice to use them while working with tuples in python.
Unlike list data structure, a tuple is immutable. Hence, you cannot add, update or delete the elements of the tuple after creating it. However, if you want to perform any operations on the tuple, a separate copy of the same tuple is created, and then necessary modifications are made. Below example shows a tuple in python programming:
For Example
tuple1 = ("favtutor", 1, 2.3) print(tuple1) # Creating a tuple having one element tuple2 = ("favtutor",) print(type(tuple2))
Output
As shown in the above example, when you declare a tuple with a single element, it will be similar to the string class. Therefore, to avoid this issue, a single comma is placed after declaring a single string to identify the tuple class.
Difference between List and Tuple
Operations are performed better
Elements can be accessed faster
Consists of various built-in methods
Do not consist of any built-in methods
Iterations are time-consuming in list
Iterations are faster in the tuple
Convert List to Tuple in Python
As discussed above, the programming requires many operations to be performed on the data. Because the properties and nature of every data structure differ from one another, you have to convert the variable from one data type to another at some point. Similarly, below we have mentioned three common and most widely used methods to convert lists to a tuple in python programming.
1) Using tuple() builtin function
Python language consists of various built-in functions to ease your programming and the tuple() function is one of them. tuple () function can take any iterable as an argument and convert it into a tuple object. As you wish to convert a python list to a tuple, you can pass the entire list as a parameter within the tuple() function, and it will return the tuple data type as an output. Check out the example below for converting “sample_list” into a tuple for better understanding.
For Example
sample_list = ['Compile', 'With', 'Favtutor'] #convert list into tuple tuple1 = tuple(sample_list) print(tuple1) print(type(tuple1))
Here, if you notice, we have used the type() function to identify the variable type that returns tuple object as an output.
2) Using loop inside the tuple
This method is a small variation of the above-given approach. You can use a loop inside the built-in function tuple() to convert a python list into a tuple object. However, it is the least used method for type conversion in comparison to others. Take a look at the below example to understand it in detail:
For Example
sample_list = ['Compile', 'With', 'Favtutor'] tuple1 = tuple(i for i in sample_list) print(tuple1)
3) Unpack list inside the parenthesis
To convert a list to a tuple in python programming, you can unpack the list elements inside the parenthesis. Here, the list essentially unpacks the elements inside the tuple literal, which is created by the presence of a single comma(,). However, this method is faster in comparison to others, but it suffers from readability which is not efficient enough.
For Example
sample_list = ['Compile', 'With', 'Favtutor'] #unpack list items and form tuple tuple1 = (*sample_list,) print(tuple1) print(type(tuple1))
Conclusion
Even though List and Tuple are different data structures in python, it is important to get familiar with their similarities and differences for using them while programming. This article helps you to understand the list and tuple in detail, along with their differences and some common methods to convert python list to tuple. If you wish to study more about type casting in python, check out our article on 6 ways to convert string to float.
Convert List to Tuple in Python
There are multiple scenarios when we want a specific data type in our programs. Lists are mutable, but it is slow to iterate over them. On the other hand, the tuples are immutable , but it is much faster to iterate over the items in the tuple. Sometimes it is a good idea to use the properties and nature of the list, whereas, in a few scenarios, the properties and nature of tuples will help to optimize the program.
We can convert one data type into another between the execution to get the advantage of both data types in a single program.
How To Convert A List To A Tuple In Python?
Both lists and tuples have different natures and properties, which gives certain advantages in certain conditions. By converting one data type into another, we can take advantage of the properties and nature of both data structures. We can use the tuple constructor to convert the list to tuple , we can also use the for loop to iterate over each list item and add it one by one into a tuple, or make use of list unpacking inside parenthesis for conversion. All the methods above to convert the list to a tuple in python are explained below with a coding example.
Method — 1 : Using tuple builtin function
The following coding example shows how to use the built-in tuple function in python to convert the list into a tuple. This tuple function is also known as tuple constructor , which is used to construct or create the tuple in python.
As we can see in the above coding example, the first list is created with some random values, then converted to the tuple using the inbuilt tuple method as used on line number 6 . At the start and the end, we printed the type of myList and myTuple after conversion, which clearly shows that list got converted to the tuple.
Method — 2: Using loop inside the tuple
The following coding example shows how to convert the list to a tuple in python using a loop inside a tuple. Here we just added the for loop inside the inbuilt tuple function to iterate over the elements from the list.
As we can see, the above example is similar to method 1 . Still, the slight difference is that we added the for loop inside the tuple function to iterate over each element from the myList in the tuple function and add them inside the myTuple.
Method — 3 : Using list
The following coding example shows how to convert a list to a tuple in python with the help of list unpacking inside a parenthesis . This method is a little faster than others due to the efficient internal opcode sequence of list unpacking in python, but it is not good if readability is concerned.
As shown in the above coding example, we unpacked the list inside the parenthesis to convert it to a tuple in python. The list gets converted to a tuple due to the presence of ‘,’ in parenthesis. Here we used myList in which ‘*’ will unpack the myList, and because we used the ‘,’ after myList, all unpacked values converted to the tuple.
Conclusion
- To utilize the properties and nature of both lists and tuples in a single program, we can convert one into another.
- Inbuilt tuple() function, also known as a constructor function, can be used to convert a list to a tuple.
- We can use for loop inside the tuple function to convert the list into a tuple.
- It is faster to convert the list to a tuple in python by unpacking the list inside parenthesis.