Bird
Raised Fist0
Pythonprogramming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the enumerate() function do in Python?
easy
A. It counts how many items are in a list.
B. It sorts the items in a list alphabetically.
C. It reverses the order of items in a list.
D. It returns both the index and the value of items in a list during a loop.

Solution

  1. Step 1: Understand the purpose of enumerate()

    The enumerate() function adds a counter to an iterable and returns it as an enumerate object.
  2. Step 2: Identify what is returned during looping

    When looping, it gives both the index (position) and the value of each item.
  3. Final Answer:

    It returns both the index and the value of items in a list during a loop. -> Option D
  4. Quick Check:

    enumerate() = index + value [OK]
Hint: Remember: enumerate() pairs index with value [OK]
Common Mistakes:
  • Thinking enumerate() sorts the list
  • Assuming it only counts items
  • Believing it reverses the list
2. Which of the following is the correct syntax to use enumerate() in a for loop?
easy
A. for index in enumerate(list):
B. for value, index in enumerate(list):
C. for index, value in enumerate(list):
D. for value in enumerate(list):

Solution

  1. Step 1: Recall the unpacking order of enumerate()

    enumerate() returns pairs of (index, value), so the loop variables must match this order.
  2. Step 2: Match the correct variable order in the for loop

    The correct syntax is for index, value in enumerate(list): to unpack both index and value properly.
  3. Final Answer:

    for index, value in enumerate(list): -> Option C
  4. Quick Check:

    Index comes first, then value [OK]
Hint: Index always comes before value in enumerate() [OK]
Common Mistakes:
  • Swapping index and value order
  • Using only one variable to unpack both
  • Forgetting to unpack both values
3. What is the output of this code?
fruits = ['apple', 'banana', 'cherry']
for i, fruit in enumerate(fruits, start=1):
    print(i, fruit)
medium
A. 1 apple 2 banana 3 cherry
B. 0 apple 1 banana 2 cherry
C. apple 0 banana 1 cherry 2
D. apple 1 banana 2 cherry 3

Solution

  1. Step 1: Understand the start parameter in enumerate()

    The start=1 means counting begins at 1 instead of the default 0.
  2. Step 2: Trace the loop output

    Loop prints index and fruit: 1 apple, 2 banana, 3 cherry.
  3. Final Answer:

    1 apple 2 banana 3 cherry -> Option A
  4. Quick Check:

    start=1 shifts index to 1 [OK]
Hint: Check start value to know index numbering [OK]
Common Mistakes:
  • Assuming index starts at 0 despite start=1
  • Mixing up index and value order in print
  • Ignoring the start parameter
4. Find the error in this code snippet:
items = ['pen', 'pencil', 'eraser']
for value, index in enumerate(items):
    print(index, value)
medium
A. There is no error; the code runs fine.
B. The variables in the for loop are reversed; index should come first.
C. The print statement syntax is incorrect.
D. enumerate() cannot be used with lists.

Solution

  1. Step 1: Check the order of variables unpacked from enumerate()

    enumerate() returns (index, value), so the first variable must be index.
  2. Step 2: Identify the mistake in variable order

    The code uses value, index which is reversed and causes incorrect unpacking.
  3. Final Answer:

    The variables in the for loop are reversed; index should come first. -> Option B
  4. Quick Check:

    Index always first in enumerate() unpacking [OK]
Hint: Index comes before value in enumerate() unpacking [OK]
Common Mistakes:
  • Swapping index and value variables
  • Thinking enumerate() can't be used with lists
  • Misreading the error as print syntax
5. You have a list of names: names = ['Anna', 'Bob', '', 'Diana']. You want to print only non-empty names with their positions starting at 1. Which code correctly does this using enumerate()?
hard
A. for i, name in enumerate(names, start=1): if name: print(i, name)
B. for i, name in enumerate(names): if name != '': print(i, name)
C. for i, name in enumerate(names, start=0): if name: print(i, name)
D. for i, name in enumerate(names, start=1): if name == '': print(i, name)

Solution

  1. Step 1: Indexing starting at 1

    Use enumerate(names, start=1) to get positions starting from 1: 1 Anna, 2 Bob, 3 (empty), 4 Diana.
  2. Step 2: Skip empty names

    Use if name: since empty string '' is falsy in Python.
  3. Final Answer:

    for i, name in enumerate(names, start=1): if name: print(i, name) -> Option A
  4. Quick Check:

    start=1 + if name skips empty [OK]
Hint: Use start=1 and if name to skip empty strings [OK]
Common Mistakes:
  • Forgetting to start counting at 1
  • Printing empty strings
  • Not adjusting index when using default start=0