![]() |
|
[Tut] Convert List to Tuple | The Most Pythonic Way - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Convert List to Tuple | The Most Pythonic Way (/thread-95837.html) |
[Tut] Convert List to Tuple | The Most Pythonic Way - xSicKxBot - 06-22-2020 Convert List to Tuple | The Most Pythonic Way Answer: The simplest, most straightforward, and most readable way to convert a list to a tuple is Python’s built-in Code: tuple(list)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. Code: lst = [1, 2, 3]You want to create a new tuple data structure that contains the same integer elements: Code: (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: https://trinket.io/embed/python/dc9e315394 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 Code: tuple(iterable)![]() Here’s an example: Code: lst = [1, 2, 3]
t = tuple(lst)The result is the following tuple: Code: print(t)
# (1, 2, 3)This is the most Pythonic way if a flatconversion of a single list to a tuple is all you need. But what if you want to convert multiple lists to a tuple? 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 readabilityfor beginner coders). Sounds interesting? Let’s dive into unpacking and the asterisk operator! The asterisk operator Code: *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. Code: t = (*lst,)
print(t)
# (1, 2, 3)You unpack all elements in the Code: lstCode: (*lst,)Code: (*lst)The strength of this approach is—despite being even conciser than the standard Code: tuple(...)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: Code: lst_1 = [1, 2, 3]
lst_2 = [4, 5, 6]
t = (*lst_1, *lst_2)
print(t)
# (1, 2, 3, 4, 5, 6)The expression Code: (*lst_1, *lst_2)Code: lst_1Code: lst_2Method 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: Code: 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: Code: 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 Code: tuple(...)Code: (...,)Related articles: I hope you liked the article! Feel free to join our large community of ambitious Python coders and download your free Python resources (PDF cheat sheets). 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. Join the free webinar now! |