Answer: The simplest, most straightforward, and most readable way to convert a list to a tuple is Python’s built-in
tuple(list) function. You can pass any iterable (such as a list, another tuple, or a set) as an argument into this so-called constructor function and it returns a new tuple data structure that contains all elements of the iterable.
Converting a list to a tuple seems trivial, I know. But keep reading and I’ll show you surprising ways of handling this problem. I guarantee that you’ll learn a lot of valuable things from the 3-5 minutes you’ll spend reading this tutorial! 🙂
Problem: Given a list of elements. Create a new tuple with the same elements—thereby converting the list to a tuple.
Example: You have the following list.
lst = [1, 2, 3]
You want to create a new tuple data structure that contains the same integer elements:
(1, 2, 3)
Let’s have a look at the different ways to convert a list to a tuple—and discuss which is the most Pythonic way in which circumstance.
You can get a quick overview in the following interactive code shell. Explanations for each method follow after that:
Exercise: Run the code. Skim over each method—does any of them confuse you?
Method 1: Tuple Constructor
The simplest, most straightforward, and most readable way to convert a list to a tuple is Python’s built-in
tuple(iterable) function. You can pass any iterable (such as a list, another tuple, or a set) as an argument into this so-called constructor function and it returns a new tuple data structure that contains all elements of the iterable.
Here’s an example:
lst = [1, 2, 3] t = tuple(lst)
The result is the following tuple:
print(t) # (1, 2, 3)
Method 2: Unpacking
There’s an alternative that works for one or more lists. In other words, you can use this general method to convert one or more lists into a tuple. This method is equally efficient and it takes less characters than Method 1 (at the costs of readability for beginner coders). Sounds interesting? Let’s dive into unpacking and the asterisk operator!
The asterisk operator
* is also called “star operator” and you can use it as a prefix on any list. The operator will “unpack” all elements of the list into the outer structure—so make sure you call it only within argument lists or within an enclosing container type such as a list or a tuple.
Here’s how it works to unpack all elements of a list into an enclosing tuple—thereby converting the original list to a new tuple.
t = (*lst,) print(t) # (1, 2, 3)
You unpack all elements in the
lst into the outer structure
(. The outer parentheses with the comma indicate that it’s a tuple and not the parentheses of a simple expression. If you’d forget to add the comma, Python would interpret the expression
(*lst) as being a simple precedence relation and it would throw an error because there’s no outer container data structure that “catches” the unpacked elements.
The strength of this approach is—despite being even conciser than the standard
tuple(...) function—that you can unpack multiple values into it!
Method 3: Unpacking to Convert Multiple Lists to a Single Tuple
Let’s have a look at how you’d create a tuple from multiple lists:
lst_1 = [1, 2, 3] lst_2 = [4, 5, 6] t = (*lst_1, *lst_2) print(t) # (1, 2, 3, 4, 5, 6)
(*lst_1, *lst_2) unpacks all elements in
lst_2 into the outer tuple structure. This allows you to convert multiple lists to a single tuple.
Method 4: Generator Expression to Convert Multiple Lists to Tuple
If you have multiple lists stored in a list of lists and you want to convert them to a single tuple, you can use a short generator expression statement to go over all inner lists and over all elements of each inner list. Then, you place each of those elements into the tuple structure:
lst = [[1, 2], [3, 4], [5, 6, 7]] t = tuple(x for l in lst for x in l) print(t) # (1, 2, 3, 4, 5, 6, 7)
This is the most Pythonic way to convert a list of lists to a tuple. It’s short and efficient and readable. You don’t create any helper data structure that takes space in memory.
But what if you want to save a few more characters?
Method 5: Generator Expression + Unpacking
Okay, you shouldn’t do this last method using the asterisk operator—it’s unreadable—but I couldn’t help including it here:
lst = [[1, 2], [3, 4], [5, 6, 7]] t = (*(x for l in lst for x in l),) print(t) # (1, 2, 3, 4, 5, 6, 7)
Rather than using the
tuple(...) function to convert the generator expression to a tuple, you use the
(...,) helper structure to indicate that it’s a tuple you want—and unpack all elements from the generator expression into the tuple. Sure, it’s not very readable—but you could see such a thing in practice (if pro coders want to show off their skills ;)).
I hope you liked the article!
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.