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
Recall & Review
beginner
What does the enumerate() function do in Python?
It adds a counter to an iterable (like a list) and returns it as an enumerate object, which yields pairs of (index, item).
Click to reveal answer
beginner
How do you use enumerate() with a list fruits = ['apple', 'banana', 'cherry'] to get index and fruit?
Use a for loop: for index, fruit in enumerate(fruits): print(index, fruit)
Click to reveal answer
intermediate
What is the purpose of the optional start parameter in enumerate()?
It sets the starting index number. By default, it starts at 0, but you can change it to any integer.
Click to reveal answer
intermediate
What type of object does enumerate() return?
It returns an enumerate object, which is an iterator that produces pairs of (index, item) when looped over.
Click to reveal answer
beginner
Why is enumerate() better than using a manual counter variable in a loop?
Because it is cleaner, less error-prone, and makes the code easier to read by automatically handling the index counting.
Click to reveal answer
What does enumerate(['a', 'b', 'c']) produce when converted to a list?
A[(1, 'a'), (2, 'b'), (3, 'c')]
B['a', 'b', 'c']
C[(0, 'a'), (1, 'b'), (2, 'c')]
D['0a', '1b', '2c']
✗ Incorrect
The default start index is 0, so enumerate pairs each item with its index starting at 0.
How can you start enumeration at 1 instead of 0?
Aenumerate(iterable, start=1)
Benumerate(iterable, 1)
Cenumerate(iterable).start(1)
Denumerate(iterable).index(1)
✗ Incorrect
The start parameter sets the starting index, so use enumerate(iterable, start=1).
Which of these is a correct way to get index and value from a list nums?
Afor i in enumerate(nums): val = i
Bfor i, val in enumerate(nums):
Cfor val, i in enumerate(nums):
Dfor i in nums.enumerate():
✗ Incorrect
The enumerate function returns (index, value) pairs, so unpack as i, val.
What type of object is returned by enumerate()?
AEnumerate object (iterator)
BTuple
CDictionary
DList
✗ Incorrect
enumerate() returns an iterator object that yields index-item pairs.
Why might you prefer enumerate() over a manual counter variable?
AIt automatically handles counting and is less error-prone.
BIt runs faster than any loop.
CIt creates a dictionary automatically.
DIt sorts the list while looping.
✗ Incorrect
enumerate() simplifies code by managing the index count automatically.
Explain how the enumerate() function works and give an example of its use.
Think about how you get both position and value when looping.
You got /3 concepts.
Describe the purpose of the start parameter in enumerate() and how to use it.
It lets you choose where counting begins.
You got /3 concepts.
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
Step 1: Understand the purpose of enumerate()
The enumerate() 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 D
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
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 is for index, value in enumerate(list): to unpack both index and value properly.
Final Answer:
for index, value in enumerate(list): -> Option C
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
Step 1: Understand the start parameter in enumerate()
The start=1 means 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 A
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
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 uses value, index which is reversed and causes incorrect unpacking.
Final Answer:
The variables in the for loop are reversed; index should come first. -> Option B
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
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.
Step 2: Skip empty names
Use if name: since empty string '' is falsy in Python.
Final Answer:
for i, name in enumerate(names, start=1):
if name:
print(i, name) -> Option A
Quick Check:
start=1 + if name skips empty [OK]
Hint: Use start=1 and if name to skip empty strings [OK]