0
0
Pythonprogramming~20 mins

enumerate() function in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Enumerate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of enumerate with start index
What is the output of this Python code?
Python
items = ['apple', 'banana', 'cherry']
result = list(enumerate(items, start=1))
print(result)
A[(0, 'apple'), (1, 'banana'), (2, 'cherry')]
B[(1, 'apple'), (2, 'banana'), (3, 'cherry')]
C[(1, 'apple'), (1, 'banana'), (1, 'cherry')]
D[(0, 'apple'), (0, 'banana'), (0, 'cherry')]
Attempts:
2 left
๐Ÿ’ก Hint
Remember that enumerate can start counting from any number you choose.
โ“ Predict Output
intermediate
2:00remaining
Using enumerate in a for loop
What will this code print?
Python
colors = ['red', 'green', 'blue']
for i, color in enumerate(colors):
    if i == 2:
        print(color)
Ablue
Bgreen
Cred
DIndexError
Attempts:
2 left
๐Ÿ’ก Hint
The loop prints the color when the index is 2.
๐Ÿง  Conceptual
advanced
2:00remaining
Behavior of enumerate on empty iterable
What is the result of converting enumerate([]) to a list?
A[(1, None)]
B[(0, None)]
C[]
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens when you enumerate over an empty list.
โ“ Predict Output
advanced
2:00remaining
Output of enumerate with tuple unpacking
What does this code print?
Python
data = ['a', 'b', 'c']
for index, value in enumerate(data, 5):
    if index == 6:
        print(value)
Aa
BIndexError
Cc
Db
Attempts:
2 left
๐Ÿ’ก Hint
The start index is 5, so the first item has index 5.
โ“ Predict Output
expert
2:00remaining
Effect of modifying list during enumerate
What happens when you run this code?
Python
lst = [10, 20, 30]
for i, val in enumerate(lst):
    if val == 20:
        lst.append(40)
print(len(lst))
A4
B3
CRuntimeError
DInfinite loop
Attempts:
2 left
๐Ÿ’ก Hint
Think about how enumerate works with the list size changing during iteration.