0
0
Pythonprogramming~10 mins

enumerate() function in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - enumerate() function
Start with iterable
Call enumerate(iterable)
Create index=0
For each item in iterable
Yield (index, item)
Increment index by 1
Back to For each item
The enumerate() function takes an iterable and returns pairs of (index, item) starting from zero, increasing the index by one each time.
Execution Sample
Python
fruits = ['apple', 'banana', 'cherry']
for i, fruit in enumerate(fruits):
    print(i, fruit)
This code prints each fruit with its index from the list.
Execution Table
Iterationi (index)fruit (item)ActionOutput
10'apple'Yield (0, 'apple')0 apple
21'banana'Yield (1, 'banana')1 banana
32'cherry'Yield (2, 'cherry')2 cherry
4--No more items, loop ends-
💡 All items in the list have been processed, so the loop stops.
Variable Tracker
VariableStartAfter 1After 2After 3Final
i-0122
fruit-'apple''banana''cherry''cherry'
Key Moments - 3 Insights
Why does the index start at 0 and not 1?
By default, enumerate() starts counting from 0 as shown in the execution_table rows 1-3. You can change this by passing a second argument to enumerate(), but if you don't, it always starts at 0.
What happens if the iterable is empty?
If the iterable is empty, the loop does not run at all, as shown in the exit_note where no items means no iterations.
Can we change the starting index?
Yes, you can pass a second argument to enumerate(), like enumerate(fruits, 1), to start counting from 1 instead of 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'i' at iteration 2?
A0
B1
C2
D3
💡 Hint
Check the 'i (index)' column at iteration 2 in the execution_table.
At which iteration does the loop stop according to the execution_table?
AAfter iteration 3
BAfter iteration 1
CAfter iteration 4
DIt never stops
💡 Hint
Look at the exit_note and the last row of the execution_table.
If we change enumerate(fruits) to enumerate(fruits, 1), what will be the output at iteration 1?
A1 apple
B0 apple
C2 apple
DError
💡 Hint
Recall that the second argument to enumerate() sets the starting index.
Concept Snapshot
enumerate(iterable, start=0)
- Returns pairs (index, item) from iterable
- Index starts at 0 by default
- Useful to get item position in loops
- Can change start index with second argument
- Stops when iterable is exhausted
Full Transcript
The enumerate() function in Python takes an iterable like a list and returns pairs of an index and the item from the iterable. The index starts at zero by default and increases by one for each item. In the example, we loop over a list of fruits and print the index and fruit name. The execution table shows each step: the index and item yielded, and the output printed. The loop stops when all items are processed. You can change the starting index by passing a second argument to enumerate(). This helps when you want to count from 1 or another number instead of zero.