Brain Structural Validation / Validate Markdown Structure & Links (push) Failing after 8s
187 lines
6.3 KiB
Markdown
187 lines
6.3 KiB
Markdown
---
|
|
type: knowledge
|
|
status: stable
|
|
topics: ['python', 'iteration']
|
|
---
|
|
|
|
# Quick Guide to Python's `zip()`
|
|
If you spend enough time writing Python, you will eventually find yourself needing to loop over two or more related lists at the same time.
|
|
|
|
Imagine you are processing user data and have two separate lists—one for names and one for test scores:
|
|
|
|
```python
|
|
names = ["Ana", "Daniela", "Luisa"]
|
|
scores = [90, 72, 92]
|
|
```
|
|
|
|
You want the output to match them up logically:
|
|
|
|
```text
|
|
Ana scored 90
|
|
Daniela scored 72
|
|
Luisa scored 92
|
|
```
|
|
|
|
Naturally, the first question is: *"How do I pull items from both lists simultaneously?"*
|
|
|
|
## The First Solution Most People Write
|
|
If you are coming from a language like C, C++ or Java, your instinct is to use an index to keep track of your position, then ask both lists for the item at that position:
|
|
|
|
```python
|
|
for i in range(len(names)):
|
|
print(f"{names[i]} scored {scores[i]}")
|
|
```
|
|
|
|
This works, and it's logically correct. However, it introduces unnecessary complexity. You are forcing yourself to manage the mechanics of an index `i` that you do not actually care about. The index is merely a middleman to get the data you want. Furthermore, if `names` and `scores` happen to be of unequal lengths, this approach risks throwing an `IndexError`.
|
|
|
|
What if Python could weave these lists together for you?
|
|
|
|
## Introducing `zip()`
|
|
|
|
Python provides the built-in function `zip()` exactly for this scenario. Instead of managing an index, you hand the lists directly to `zip()`, which pairs them up.
|
|
|
|
If we convert the iterator to a list to peek inside, we obtain:
|
|
|
|
```python
|
|
list(zip(names, scores))
|
|
# [('Luisa', 90), ('Nathalia', 72), ('Daniela', 92), ('Elizabeth', 82), ('Camila', 84), ('Jennifer', 40)]
|
|
```
|
|
|
|
This reinforces the idea that `zip()` is lazy—it returns an iterator, which we must cast to a list to view all at once.
|
|
|
|
If we loop over the `zip()` iterator directly, we see that every iteration yields a paired tuple:
|
|
|
|
```python
|
|
for pair in zip(names, scores):
|
|
print(pair)
|
|
```
|
|
|
|
Output:
|
|
```text
|
|
('Luisa', 90)
|
|
('Nathalia', 72)
|
|
('Daniela', 92)
|
|
...
|
|
```
|
|
|
|
## The Role of Tuple Unpacking
|
|
|
|
Only after seeing that `zip()` yields tuples does it become clear how we can use **tuple unpacking** to clean up the loop. Python can split the tuple directly in the loop header:
|
|
|
|
```python
|
|
for name, score in zip(names, scores):
|
|
print(f"{name} scored {score}")
|
|
```
|
|
|
|
Every time the loop spins, Python instantly splits the yielded tuple into the individual variables you defined:
|
|
|
|
```txt
|
|
('Luisa', 90)
|
|
↓
|
|
name = "Luisa"
|
|
score = 90
|
|
```
|
|
|
|
This completely removes the need for an index variable or bracket notation like `names[i]`. You are interacting directly with the data elements themselves.
|
|
|
|
## The Magic of Unzipping with `*` (Iterable unpacking)
|
|
One question beginners often have is how to reverse the operation.
|
|
|
|
Suppose you have a list of paired records from a database query, and you need to split them back into two separate lists:
|
|
|
|
```python
|
|
records = [("Luisa", 85), ("Nathalia", 92), ("Angie", 78)]
|
|
```
|
|
|
|
You can use `zip()` in conjunction with the unpacking operator `*` to pull them apart:
|
|
|
|
```python
|
|
names, scores = zip(*records)
|
|
|
|
print(names)
|
|
# ('Luisa', 'Nathalia', 'Angie')
|
|
print(scores)
|
|
# (85, 92, 78)
|
|
```
|
|
|
|
**How does this work?** The `*` operator (unpacking operator) unpacks the `records` iterable into separate positional arguments before `zip()` executes. If you're coming from JavaScript, you'll notice the resemblance to the spread operator (`...`): they take a collection of items and "blast them out" into their individual elements.
|
|
|
|
```python
|
|
numbers = [2, 5, 1]
|
|
print(numbers)
|
|
# [2, 5, 1]
|
|
print(*numbers)
|
|
# 2 5 1
|
|
```
|
|
|
|
Writing `zip(*records)` is exactly equivalent to writing:
|
|
|
|
```python
|
|
zip(records[0], records[1], records[2])
|
|
|
|
# which means:
|
|
zip(("Luisa", 85), ("Nathalia", 92), ("Angie", 78))
|
|
```
|
|
|
|
`zip()` takes the first item from every tuple (`"Luisa"`, `"Nathalia"`, `"Angie"`), and groups them. Then it takes the second item from every tuple (`85`, `92`, `78`) and groups them. The result is two separate tuples.
|
|
|
|
## When should I use it?
|
|
To decide whether `zip()` is the right tool, ask yourself:
|
|
|
|
> ***Do I need to process multiple sequences in parallel?***
|
|
|
|
If the answer is yes, `zip()` is the correct choice. Here a few common scenarios:
|
|
|
|
**1. Creating Dictionaries:**
|
|
Fusing two lists into a key-value mapping. This avoids manual iteration entirely.
|
|
```python
|
|
keys = ["username", "email", "role"]
|
|
values = ["kiskaadee", "admin@system.local", "superadmin"]
|
|
|
|
user_profile = dict(zip(keys, values))
|
|
```
|
|
|
|
**2. Multi-Variable Calculation**:
|
|
Calculating results from corresponding elements in multiple data sets.
|
|
```python
|
|
prices = [10.00, 15.00, 20.00]
|
|
quantities = [2, 1, 4]
|
|
|
|
total_cost = sum(price * qty for price, qty in zip(prices, quantities))
|
|
```
|
|
|
|
**3. Transposing Matrices**:
|
|
Pivoting rows into columns (or vice versa) in a 2D grid.
|
|
```python
|
|
matrix = [
|
|
[1, 2, 3],
|
|
[4, 5, 6]
|
|
]
|
|
|
|
transposed = list(zip(*matrix))
|
|
```
|
|
|
|
## The Mental Picture
|
|
To fully grasp `zip()`, visualize a literal zipper on a jacket.
|
|
1. One sequence represents the teeth on the left
|
|
2. The other sequence represents the teeth on the right
|
|
3. As you pull the slider down, it locks the corresponding left and right teeth together into a single unit.
|
|
|
|
Like `enumerate()`, `zip()` is lazy. It does not construct a massive list of tuples in memory all at once. It yields one pair at a time, making it highly efficient for processing massive files or data streams.
|
|
|
|
## The Shortest-Length Trade-Off
|
|
A real jacket zipper stops working if one side is missing teeth. Python's `zip()` behaves similarly: it stops as soon as the *shortest* iterable is exhausted. Any remaining items in the longer list are silently ignored.
|
|
|
|
If losing data silently is a risk for your application, you have two options to manage this trade-off:
|
|
- Use `zip(a, b, strict=True)` (Python 3.10+), which intentionally throws an error if the lists are different lengths.
|
|
- Use `itertools.zip_longest()`, which pads the missing spaces with `None` (or a filler value) instead of truncating.
|
|
|
|
## One Principle to Remember
|
|
You do not need to memorize edge cases to start using this tool. Rely on this heuristic:
|
|
|
|
Whenever you find yourself writing `[i]` multiple times inside a loop to access elements from different lists, pause and ask yourself:
|
|
|
|
> ***"Am I trying to move through these lists at the same time?"***
|
|
|
|
If the answer is yes, consider using `zip()`.
|