docs(knowledge): add guide to Python enumerate
This commit is contained in:
1 parent
c96f00e439
commit
781fbb20b0
1 file changed
+188
@@ -0,0 +1,188 @@
|
||||
# Quick guide to Python's `enumerate()`
|
||||
|
||||
If you spend enough time writing Python, you will eventually find yourself needing to loop over a list while keeping track of where you are in that list.
|
||||
|
||||
Imagine you are building a simple command-line application and you want to print a numbered menu of options:
|
||||
|
||||
Python
|
||||
|
||||
```python
|
||||
fruits = ["apple", "banana", "orange"]
|
||||
```
|
||||
|
||||
You want the output to look like this:
|
||||
|
||||
```Plaintext
|
||||
1. apple
|
||||
2. banana
|
||||
3. orange
|
||||
```
|
||||
|
||||
Naturally, the first question most beginners ask is: _"How do I know which iteration I'm currently on?"_
|
||||
|
||||
## The First Solution Most People Write
|
||||
|
||||
If you are new to Python or coming from another language, your first instinct is usually to set up a manual counter outside the loop and update it inside.
|
||||
|
||||
|
||||
```python
|
||||
index = 1
|
||||
|
||||
for fruit in fruits:
|
||||
print(f"{index}. {fruit}")
|
||||
index += 1
|
||||
```
|
||||
|
||||
This works perfectly. In fact, there is nothing logically wrong with it. The code runs, the output is correct, and the intent is fairly clear.
|
||||
|
||||
The only downside is that you are now forcing your brain—and the computer—to manage two distinct concepts at once:
|
||||
|
||||
1. Moving through the items in the list.
|
||||
|
||||
2. Manually updating a numerical counter.
|
||||
|
||||
|
||||
What if Python could keep track of the counter for us?
|
||||
|
||||
## Meeting `enumerate`
|
||||
|
||||
Python provides a built-in tool specifically designed for this scenario. Instead of managing the counter yourself, you can hand the list to `enumerate`.
|
||||
|
||||
|
||||
```Python
|
||||
for index, fruit in enumerate(fruits, start=1):
|
||||
print(f"{index}. {fruit}")
|
||||
```
|
||||
|
||||
To understand why this is so powerful, we need to look at what `enumerate` is actually doing behind the scenes. `enumerate` walks through your list exactly as a normal `for` loop would. The only difference is that, before handing you the item, it pairs that item with a running number.
|
||||
|
||||
If you were to peek inside `enumerate(fruits)`, the sequence looks like this:
|
||||
|
||||
|
||||
```Plaintext
|
||||
(0, "apple")
|
||||
(1, "banana")
|
||||
(2, "orange")
|
||||
```
|
||||
|
||||
_(Note: In programming, counting almost always starts at 0. If you want human-readable numbers like our menu example, you just pass `start=1` as we did above)._
|
||||
|
||||
### Why are there two variables?
|
||||
|
||||
When you write `for index, fruit in enumerate(...)`, you are telling Python to unpack that pair of data on the fly.
|
||||
|
||||
Every time the loop spins, `enumerate` hands over a tuple. Python instantly splits that tuple into the two variables you asked for:
|
||||
|
||||
|
||||
```Plaintext
|
||||
(0, "apple")
|
||||
↓
|
||||
index = 0
|
||||
fruit = "apple"
|
||||
```
|
||||
|
||||
This completely removes the need to increment a number at the bottom of your loop. Python handles the bookkeeping.
|
||||
|
||||
## Why not just use `range(len())`?
|
||||
|
||||
If you have spent time in languages like C or Java, you might be tempted to use the length of the list to generate indices, and then use those indices to look up the items:
|
||||
|
||||
|
||||
```Python
|
||||
|
||||
for i in range(len(fruits)):
|
||||
print(f"{i}. {fruits[i]}")
|
||||
```
|
||||
|
||||
Compare that directly to the `enumerate` approach:
|
||||
|
||||
|
||||
```Python
|
||||
for i, fruit in enumerate(fruits):
|
||||
print(f"{i}. {fruit}")
|
||||
```
|
||||
|
||||
Read both of those aloud. Which one tells you what you are actually iterating over?
|
||||
|
||||
The first approach focuses entirely on the mechanics of the computer: find the length, create a range of numbers, iterate over the numbers, and use the number to access the list. The second approach focuses on your actual goal: get the position and the fruit.
|
||||
|
||||
## When should I use it?
|
||||
|
||||
You don't need `enumerate` for every loop. To decide if it is the right tool, ask yourself one question:
|
||||
|
||||
**"Do I need both the item and its position?"**
|
||||
|
||||
If the answer is yes, `enumerate` is usually the right choice. Here are a few common scenarios:
|
||||
|
||||
**1. Numbered Menus**
|
||||
|
||||
Displaying human-readable lists to users.
|
||||
|
||||
|
||||
|
||||
```Python
|
||||
for number, option in enumerate(menu, start=1):
|
||||
print(f"Press {number} for {option}")
|
||||
```
|
||||
|
||||
**2. Finding an Item's Exact Location**
|
||||
|
||||
Searching for a specific element and returning its index.
|
||||
|
||||
|
||||
```Python
|
||||
for index, user in enumerate(users):
|
||||
if user.id == target_id:
|
||||
print(f"User found at row {index}")
|
||||
```
|
||||
|
||||
**3. Updating a List in Place**
|
||||
|
||||
When you need to modify the original list based on the items currently inside it.
|
||||
|
||||
|
||||
|
||||
```Python
|
||||
for i, value in enumerate(numbers):
|
||||
numbers[i] = value * 2
|
||||
```
|
||||
|
||||
## The Mental Picture
|
||||
|
||||
To truly understand `enumerate`, it helps to know that it does not build a brand-new list of tuples in your computer's memory. That would be highly inefficient for very large datasets.
|
||||
|
||||
Instead, it is "lazy." It creates an iterator that produces one `(index, value)` pair at a time, exactly when the loop asks for it.
|
||||
|
||||
Imagine someone walking beside your list carrying a mechanical tally clicker. Every time your loop asks for the next item:
|
||||
|
||||
1. The clicker increases by one.
|
||||
|
||||
2. The item is handed to you.
|
||||
|
||||
3. You receive both together.
|
||||
|
||||
|
||||
|
||||
|
||||
```Plaintext
|
||||
apple ← the item
|
||||
0 ← the click
|
||||
|
||||
banana
|
||||
1
|
||||
|
||||
orange
|
||||
2
|
||||
```
|
||||
|
||||
Because of this design, `enumerate` doesn't care how long the list is, or even if it's a list at all. It works perfectly with files, generators, and any other sequence in Python.
|
||||
|
||||
## One Principle to Remember
|
||||
|
||||
You do not need to memorize a list of strict rules for when to use `enumerate`. Instead, leave with this heuristic:
|
||||
|
||||
Whenever you find yourself writing a counter variable whose only purpose is to keep track of the current iteration, pause and ask:
|
||||
|
||||
_"Would `enumerate` do this for me?"_
|
||||
|
||||
Almost every time, the answer will be yes.
|
||||
Reference in new issue
Block a user