The enumerate() function helps you get both the position (index) and the value from a list or other collection while looping. It makes counting items easy and clear.
enumerate() function in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
enumerate(iterable, start=0)
iterable is any collection you want to loop over, like a list or string.
start is optional and sets the first index number (default is 0).
Examples
Python
for index, value in enumerate(['apple', 'banana', 'cherry']): print(index, value)
Python
for i, letter in enumerate('hello', start=1): print(i, letter)
Sample Program
This program prints a numbered list of fruits starting from 1.
Python
fruits = ['apple', 'banana', 'cherry'] for index, fruit in enumerate(fruits, start=1): print(f"{index}. {fruit}")
Important Notes
You can use enumerate() with any iterable, not just lists.
Using start lets you control the counting number, which is helpful for user-friendly numbering.
Summary
enumerate() gives you both the index and value when looping.
It helps make loops simpler and clearer when you need item positions.
You can choose where counting starts with the start parameter.
Practice
1. What does the
enumerate() function do in Python?easy
Solution
Step 1: Understand the purpose of enumerate()
Theenumerate()function adds a counter to an iterable and returns it as an enumerate object.Step 2: Identify what is returned during looping
When looping, it gives both the index (position) and the value of each item.Final Answer:
It returns both the index and the value of items in a list during a loop. -> Option DQuick 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
Solution
Step 1: Recall the unpacking order of enumerate()
enumerate() returns pairs of (index, value), so the loop variables must match this order.Step 2: Match the correct variable order in the for loop
The correct syntax isfor index, value in enumerate(list):to unpack both index and value properly.Final Answer:
for index, value in enumerate(list): -> Option CQuick 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
Solution
Step 1: Understand the start parameter in enumerate()
Thestart=1means counting begins at 1 instead of the default 0.Step 2: Trace the loop output
Loop prints index and fruit: 1 apple, 2 banana, 3 cherry.Final Answer:
1 apple 2 banana 3 cherry -> Option AQuick 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
Solution
Step 1: Check the order of variables unpacked from enumerate()
enumerate() returns (index, value), so the first variable must be index.Step 2: Identify the mistake in variable order
The code usesvalue, indexwhich is reversed and causes incorrect unpacking.Final Answer:
The variables in the for loop are reversed; index should come first. -> Option BQuick 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
Solution
Step 1: Indexing starting at 1
Useenumerate(names, start=1)to get positions starting from 1: 1 Anna, 2 Bob, 3 (empty), 4 Diana.Step 2: Skip empty names
Useif name:since empty string '' is falsy in Python.Final Answer:
for i, name in enumerate(names, start=1): if name: print(i, name) -> Option AQuick 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
