How to Convert a List into a Tuple in Python [5 Different Methods]

Do you want to convert a list to a tuple? In this Python programming article, I will explain how to convert a list into a tuple in Python using different methods with some illustrative examples.

To convert a list into a tuple in Python, we can use the tuple() constructor for direct conversion, a for loop for element-wise addition, the unpacking (*) operator for a concise approach, list comprehension within a tuple for readability, or the map() function with a lambda for element-wise transformation.

Convert a List into a Tuple in Python

There are five different ways to convert a list into a tuple in Python:

  1. The tuple() Constructor
  2. A for loop
  3. Unpacking(*) Operator
  4. List Comprehension (Inside a Tuple)
  5. Using map() with lambda function

Let’s see all of them one by one with the help of some examples:

1. Python convert list to tuple using the tuple() constructor

This is the simplest and most direct method. We can pass the list as an argument to the tuple() function in Python, which converts and returns it as a tuple.

Here is the code to convert a list into a tuple in Python using the tuple() function:

states_visited = ['California', 'Nevada', 'Utah']
states_tuple = tuple(states_visited)
print("After conversion:", states_tuple)
print("The class is: ", type(states_tuple))

Output:

After conversion: ('California', 'Nevada', 'Utah')
The class is:  <class 'tuple'>

The following screenshot, taken after implementing the code in Pycharm, displays the result.

How to Convert a List into a Tuple in Python

2. Convert a list into a tuple using a for loop

In this method, we iterate over each element of the list in Python using a for loop and add them to an initially empty tuple using the += operator.

Here is the code to convert a list into a tuple in Python using a for loop with the += operator:

weekly_temps = [58, 60, 62, 65, 63, 61, 59]
temps_tuple = ()
for temp in weekly_temps:
    temps_tuple += (temp,)

print("After conversion:", temps_tuple)
print("The class is: ", type(temps_tuple))

Output:

After conversion: (58, 60, 62, 65, 63, 61, 59)
The class is:  <class 'tuple'>

Below is a screenshot showcasing the output after executing the code in the Pycharm editor.

List to Tuple in Python

3. Convert list to tuple in Python using unpacking(*) operator

This method involves unpacking all elements of the list using the * operator and then repacking them into a tuple in Python.

To convert a list into a tuple in Python using the * operator, one can follow the code mentioned below:

fast_food_chains = ['McDonald\'s', 'Burger King', 'Taco Bell']
chains_tuple = (*fast_food_chains,)
print("After conversion:", chains_tuple)
print("The class is: ", type(chains_tuple))

Output:

After conversion: ("McDonald's", 'Burger King', 'Taco Bell')
The class is:  <class 'tuple'>

After executing the code in Pycharm, one can see the output in the below screenshot.

How to Convert a List to a Tuple in Python

4. Convert list into tuple using list comprehension inside a tuple()

Here, we use a list comprehension inside a tuple() function. We iterate over all the elements of the list using the list comprehension then add them into an empty tuple created with the help of the tuple() constructor.

Here is the code to convert a list into a tuple in Python using list comprehension:

airports = ['LAX', 'JFK', 'ATL', 'ORD', 'DFW']
airports_tuple = tuple([airport for airport in airports])
print("After conversion:", airports_tuple)
print("The class is: ", type(airports_tuple))

Output:

After conversion: ('LAX', 'JFK', 'ATL', 'ORD', 'DFW')
The class is:  <class 'tuple'>

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

Python program to convert a list to a tuple

5. Convert a list to tuple in Python using map() with a lambda function

This method is useful when we want to apply a function(Here, lambda function) to each item of the list during the conversion. The map() function processes each list item, and the tuple() function converts the result into a tuple.

The code mentioned below demonstrates how to convert a list into a tuple in Python using the map() function with the lambda function:

publication_years = [1925, 1951, 1960, 1985]
years_tuple = tuple(map(lambda x: x, publication_years))
print("After conversion:", years_tuple)
print("The class is: ", type(years_tuple))

Output:

After conversion: (1925, 1951, 1960, 1985)
The class is:  <class 'tuple'>

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

convert list to tuple in Python

Now, let’s see some different scenarios of how to convert a list into a tuple in Python:

1. Convert list of lists to tuple of tuples in Python

A List Comprehension within a Tuple Construction is used to convert a list of lists to a tuple of tuples. It involves iterating over each element (sublist) in the original list and converting each of these sublists into a tuple. The tuple() constructor converts the entire structure into a tuple of tuples.

Here is the code to convert a list of lists to a tuple of tuples in Python:

city_coordinates = [
    ['New York', [40.7128, -74.0060]],
    ['Los Angeles', [34.0522, -118.2437]]
]
city_coordinates_tuple = tuple(tuple(city) for city in city_coordinates)
print("After conversion:", city_coordinates_tuple)
print("The class is: ", type(city_coordinates_tuple))

# Check if each item in the tuple is also a tuple
for city in city_coordinates_tuple:
    print(f"City: {city[0]}, Type: {type(city)}")

Output:

After conversion: (('New York', [40.7128, -74.006]), ('Los Angeles', [34.0522, -118.2437]))
The class is:  <class 'tuple'>
City: New York, Type: <class 'tuple'>
City: Los Angeles, Type: <class 'tuple'>

The screenshot below showcases the output after the code has been run in the Pycharm editor.

list to tuple Python

2. Change list with one element to tuple in Python

Here, we can use any of the above methods mentioned to convert a list with one element to a tuple in Python.

Here is the code to convert a list with one element to a tuple in Python:

highest_peak = ['Denali']
highest_peak_tuple = tuple(highest_peak)
print("After conversion:", highest_peak_tuple)
print("The class is: ", type(highest_peak_tuple))

Output:

After conversion: ('Denali',)
The class is:  <class 'tuple'>

Following the execution of the code in Pycharm, the resulting screenshot is presented below.

python list to tuple

Conclusion

Understanding how to convert a list into a tuple in Python is straightforward with multiple methods. Utilizing the tuple() constructor offers a direct approach, while a for loop allows for custom conversions. The unpacking (*) operator and list comprehension within a tuple provides a readable alternative. Lastly, the map() function with a lambda function caters to scenarios needing element-wise operations during conversion.

Each method caters to different use cases, enhancing flexibility in Python programming.

You may also like to read: