In this tutorial, I have explained how to create a tuple from the list in Python. As a Python developer working on various projects for my clients across the USA I came across a scenario where I needed to create a tuple where data were stored in the list. After researching I found a few effective methods to achieve this task.
Create a Tuple from the List in Python
Let us learn how to create a tuple from the list in Python.
Read How to Find the Length of a Tuple in Python?
Method 1: Use the tuple() Constructor
Here, we start with a list of US states. The tuple() function takes this list and returns a new tuple containing the same elements
states = ["California", "New York", "Texas"]
states_tuple = tuple(states)
print(states_tuple)
Output:
('California', 'New York', 'Texas')
You can refer to the below screenshot to see the output.
The tuple() constructor in Python efficiently converts a list into a tuple, as demonstrated in the example. This method is easy and maintains the order of elements. It’s particularly useful when you need an immutable sequence derived from a list.
Check out How to Split a Tuple in Python?
Method 2: Use Tuple Packing
You can also create a tuple by simply separating elements with commas inside parentheses. This is known as tuple packing:
cities = ["Los Angeles", "Chicago", "Houston"]
cities_tuple = "Los Angeles", "Chicago", "Houston" # tuple packing
print(cities_tuple)
Output:
('Los Angeles', 'Chicago', 'Houston')
You can refer to the below screenshot to see the output.
Tuple packing in Python allows you to create a tuple by listing values separated by commas, optionally within parentheses. This method is concise and doesn’t require explicitly calling the tuple() constructor. It’s particularly useful for grouping related data into a single, immutable collection.
Read How to Reverse a Tuple in Python?
Convert a List of Lists to a List of Tuples in Python
Sometimes you may have a list of lists that you want to convert into a list of tuples. You can accomplish this using a list comprehension along with the tuple() function:
coordinates = [[40.7128, -74.0060], [34.0522, -118.2437], [29.7604, -95.3698]]
coordinates_tuples = [tuple(c) for c in coordinates]
print(coordinates_tuples)
Output:
[(40.7128, -74.006), (34.0522, -118.2437), (29.7604, -95.3698)]
You can refer to the below screenshot to see the output.
To convert a list of lists into a list of tuples in Python, you can utilize a list comprehension combined with the tuple() function. This approach iterates over each sublist, converting it into a tuple, resulting in a new list where each element is a tuple. This method is efficient and maintains the original order of elements.
Check out How to Get the First Element of a Tuple in Python?
Conclusion
In this article, I have explained how to create a tuple from the list in Python. I discussed two methods to accomplish this task such as using the tuple() constructor , using tuple packing. I also covered how to convert a list of lists to a list of tuples.
You may read:
- How to Create a Python Tuple with One Element?
- How to Convert a Tuple to a String in Python?
- How to Concatenate Tuples in Python?
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn