![]() |
|
[Tut] Python Tuple Comprehension Doesn’t Exist – Use This Instead - 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] Python Tuple Comprehension Doesn’t Exist – Use This Instead (/thread-99504.html) |
[Tut] Python Tuple Comprehension Doesn’t Exist – Use This Instead - xSicKxBot - 06-04-2022 Python Tuple Comprehension Doesn’t Exist – Use This Instead 5/5 – (1 vote) Python has list comprehension and dictionary comprehension as a concise way to create a list or a dictionary by modifying an existing iterable. Python also has generator expressions that allow you to create an iterable by modifying and potentially filtering each element in another iterable and passing the result in a function, for instance. Does Python have a tuple comprehension statement? And why or why not? And what to use instead if not? This tutorial will answer all your questions but first, let’s repeat the three related concepts:
If you already know these concepts well, go ahead and skip right to the end of the tutorial! ![]() List Comprehension List comprehension is a compact way of creating lists. The simple formula is Code: [expression + context]
The example Code: [x+100 for x in range(3)]Code: [100, 101, 102]Code: lst = [x for x in range(3)]
print(lst)
# [100, 101, 102] Learn More: List Comprehension in Python — A Helpful Illustrated GuideDictionary Comprehension Dictionary Comprehension is a concise and memory-efficient way to create and initialize dictionaries in one line of Python code. It consists of two parts: expression and context.
The following example shows how to use dictionary comprehension to create a mapping from women to man: Code: men = ['Bob', 'Frank', 'Pete']
women = ['Alice', 'Ann', 'Liz'] # One-Liner Dictionary Comprehension
pairs = {w:m for w, m in zip(women, men)} # Print the result to the shell
print(pairs)
# {'Bob': 'Alice', 'Frank': 'Ann', 'Pete': 'Liz'}Also, watch the following video for a quick recap on dictionary comprehension: Learn More: Python Dictionary Comprehension: A Powerful One-Liner TutorialSet Comprehension Set comprehension is a concise way of creating sets in Python using the curly braces notation Code: {expression for element in context}For example, Code: {x for x in range(10)}Code: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}Code: s = {x for x in range(10)}
print(s)
# {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}You can optionally add a condition to restrict the context in Code: {expression for element in context if condition}For example, Code: {x for x in range(10) if x>5}Code: {6, 7, 8, 9}Code: s = {x for x in range(10) if x>5}
print(s)
# {6, 7, 8, 9} Learn More: Python Set ComprehensionGenerator Expression A generator function is a Pythonic way to create an iterable without explicitly storing it in memory. This reduces memory usage of your code without incurring any additional costs. The following generator expression shows how you can use a list-comprehension like statement but pass it into the Code: sum()Code: print(sum(random.random() for i in range(1000)))The code consists of the following parts:
This way, we still don’t store the whole list of 1000 numbers in memory but create them dynamically. There are two big advantages to using a generator:
Learn More: Python One Line GeneratorTuple Comprehension Tuple comprehension such as Code: (x+100 for x in range(3))
Quote:#python tip: Generally, lists are for looping; tuples for structs. Lists are homogeneous; tuples heterogeneous. Lists for variable length. Tuple Comprehension Alternatives You can use the following alternatives instead of tuple comprehension:
You can find those two examples in the following code snippet: Code: # Tuple Comprehension Alternative 1
t = tuple(x+100 for x in range(3))
print(t)
# (100, 101, 102) # Tuple Comprehension Alternative 2
t = (1, *[x+100 for x in range(3)])
print(t)
# (1, 100, 101, 102)Enough theory. Let’s get some practice! Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. To become more successful in coding, solve more real problems for real people. 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? You build high-value coding skills by working on practical coding projects! Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people? If your answer is YES!, consider becoming 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.If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home. Join the free webinar now! |