Convert List to Tuple in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

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.

Example:

Output :

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.

Example:

Output:

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.

Example:

Output :

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.

Learn More