Table of contents
1.
Introduction
2.
Why Convert a List to a Tuple?
3.
Using the tuple() Function
4.
Using the Unpacking Operator 
5.
Using a Loop
6.
Using a Generator Expression
7.
Convert an Empty List into a Tuple
8.
Using the tuple() Constructor
9.
Convert a Nested List into a Tuple
10.
Frequently Asked Questions
10.1.
What is the difference between a list and a tuple in Python?
10.2.
Can we convert a tuple back into a list?
10.3.
What are the benefits of using a tuple instead of a list?
11.
Conclusion
Last Updated: Dec 26, 2024
Easy

List to Tuple in Python

Introduction

In Python, lists and tuples are both used to store collections of data. However, there are key differences between them. While lists are mutable (can be modified), tuples are immutable (cannot be changed once created). Sometimes, you may need to convert a list into a tuple. 

List to Tuple in Python

In this article, we will discuss different methods to convert a list to a tuple and explain the uses of each approach. 

Why Convert a List to a Tuple?

Before we jump into how to convert a list to a tuple, let’s understand why you might need to do so. Here are a few reasons:

  • Immutability: Since tuples are immutable, they are generally faster and more memory-efficient than lists. If you don’t want your data to be modified accidentally, converting a list to a tuple is a good choice.
     
  • Hashability: Tuples are hashable, which means they can be used as keys in dictionaries, unlike lists.
     
  • Performance: Tuples are often preferred for read-only data because of their performance benefits.

Using the tuple() Function

The simplest and most direct way to convert a list to a tuple is by using the tuple() function. This function takes any iterable (like a list) and returns it as a tuple.

Example:

# List
my_list = [1, 2, 3, 4, 5]

# Converting the list to a tuple
my_tuple = tuple(my_list)

# Displaying the result
print(my_tuple)
You can also try this code with Online Python Compiler
Run Code


Output:

(1, 2, 3, 4, 5)


Explanation:
Here, we first define a list my_list containing numbers. By passing this list to the tuple() function, we convert it into a tuple. The output is the tuple (1, 2, 3, 4, 5).

Using the Unpacking Operator 

Another way to convert a list into a tuple is by using the unpacking operator *. This operator allows you to unpack the elements of the list and pass them directly into a tuple.

Example:

# List
my_list = [1, 2, 3, 4, 5]

# Using unpacking operator to convert list to tuple
my_tuple = (*my_list,)

# Displaying the result
print(my_tuple)
You can also try this code with Online Python Compiler
Run Code


Output:

(1, 2, 3, 4, 5)


Explanation:
In this example, we used the unpacking operator * to unpack the list my_list and place its elements inside a new tuple. Note that we need a comma , after the *my_list to ensure Python treats it as a tuple, not just as a plain sequence of values.

Using a Loop

Another way to convert a list to a tuple is by using a loop to iterate over the elements of the list & adding them to a new tuple. This approach gives you more control over the conversion process & allows you to perform additional operations or transformations on the elements if needed. For example:

my_list = [1, 2, 3, 4, 5]
my_tuple = tuple(item for item in my_list)
print(my_tuple)
You can also try this code with Online Python Compiler
Run Code


Output:

(1, 2, 3, 4, 5)


In this code, we define a list my_list containing the elements 1, 2, 3, 4, & 5. We then use a generator expression inside the tuple() constructor to iterate over each item in my_list & create a new tuple my_tuple with the same elements. Finally, we print my_tuple to display the resulting tuple.

Let’s look at an another example that shows how you can modify the elements during the conversion process:

my_list = [1, 2, 3, 4, 5]
my_tuple = tuple(item ** 2 for item in my_list)
print(my_tuple)
You can also try this code with Online Python Compiler
Run Code


Output:

(1, 4, 9, 16, 25)


In this case, we use a generator expression to square each item in my_list before adding it to the new tuple my_tuple. This allows us to perform transformations on the elements during the conversion process.

Note: Using a loop to convert a list to a tuple provides flexibility & allows for element-wise operations, but it may be less concise compared to using the tuple() constructor directly.

Using a Generator Expression

A generator expression can be used in combination with the tuple() function to convert a list to a tuple. This method is useful when you need to apply some condition or transformation to the list elements before converting them into a tuple.

Example:

# List
my_list = [1, 2, 3, 4, 5]

# Using a generator expression to convert list to tuple
my_tuple = tuple(x * 2 for x in my_list)

# Displaying the result
print(my_tuple)
You can also try this code with Online Python Compiler
Run Code


Output:

(2, 4, 6, 8, 10)


Explanation:
In this example, we used a generator expression x * 2 for x in my_list to double each element of the list before converting it to a tuple. The result is the tuple (2, 4, 6, 8, 10).

Convert an Empty List into a Tuple

What if you have an empty list and want to convert it into a tuple? You can still use the tuple() function, and it will return an empty tuple.

Example:

# Empty list
my_list = []

# Converting the empty list to a tuple
my_tuple = tuple(my_list)

# Displaying the result
print(my_tuple)
You can also try this code with Online Python Compiler
Run Code


Output:

()

 

Explanation:
Here, we start with an empty list. When we convert it to a tuple using the tuple() function, the result is an empty tuple ().

Using the tuple() Constructor

The most basic way to convert a list to a tuple is by using the built-in tuple() constructor. You can pass the list as an argument to tuple(), & it will return a new tuple containing the same elements as the original list. For example:

my_list = [1, 2, 3, 4, 5]
my_tuple = tuple(my_list)
print(my_tuple)
You can also try this code with Online Python Compiler
Run Code


Output:

(1, 2, 3, 4, 5)


In this code, we define a list my_list containing the elements 1, 2, 3, 4, & 5. We then pass my_list as an argument to the tuple() constructor, which creates a new tuple my_tuple with the same elements. Finally, we print my_tuple to display the resulting tuple.

The tuple() constructor works with any iterable object, not just lists. You can pass other types of iterables like strings, sets, or even other tuples to tuple(), & it will create a new tuple containing the elements from that iterable.

Convert a Nested List into a Tuple

If your list contains other lists (nested lists), converting them to a tuple will result in a nested tuple. The inner lists will become tuples inside the outer tuple.

Example:

# Nested list
my_list = [[1, 2], [3, 4], [5, 6]]

# Converting the nested list to a tuple
my_tuple = tuple(tuple(inner) for inner in my_list)

# Displaying the result
print(my_tuple)
You can also try this code with Online Python Compiler
Run Code


Output:

((1, 2), (3, 4), (5, 6))


Explanation:
In this case, the outer list my_list contains three inner lists. We use a generator expression to convert each inner list to a tuple. The result is a tuple of tuples: ((1, 2), (3, 4), (5, 6)).

Frequently Asked Questions

What is the difference between a list and a tuple in Python?

A list is mutable, meaning its elements can be changed after creation. A tuple is immutable, meaning once created, its elements cannot be modified.

Can we convert a tuple back into a list?

Yes, you can convert a tuple into a list using the list() function in a similar way to how we convert a list to a tuple.

What are the benefits of using a tuple instead of a list?

Tuples are faster and require less memory compared to lists. They are also hashable, which means they can be used as keys in dictionaries, unlike lists.

Conclusion

In this article, we covered multiple ways to convert a list to a tuple in Python. The methods include using the tuple() function, the unpacking operator *, generator expressions, and handling empty and nested lists. Each approach serves a different use case, but they all result in converting a mutable list into an immutable tuple, which has advantages like immutability and performance.

Live masterclass