Home βΊ Problems βΊ enumerate with a start value
enumerate with a start value
π§ Predict the output Topic: loops Difficulty: easy Free
What this problem practises
Loops are where off-by-one bugs live. range() excludes its stop value, the loop variable keeps its last value even after the loop ends, and break versus continue changes the flow in ways that are easy to misread when you skim.
This is an output-prediction problem: read the snippet carefully and write down exactly what it prints before anything runs. These questions are interview staples because they test whether you know the language rules, or merely recognise their syntax.
Difficulty: easy β the fix or the trick is normally a single line, and a careful read should be enough.
The challenge
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The snippet
The interactive editor pre-fills exactly this code β read it, then submit your prediction:
items = ["apple", "banana", "cherry"]
for index, item in enumerate(items, start=1):
print(index, item)Understand the rule
enumerate(items, start=1) numbers the items 1, 2, 3... instead of the default 0 β but it changes only the counter, never which items appear or how many. Each iteration yields a (count, value) pair, typically unpacked as 'for i, item in ...'. The prediction traps are all bookkeeping: which number pairs with which item, and what start does not do.
How to trace it
- Write the items in order, untouched by start β enumerate never skips or reorders.
- Attach counters beginning at start: the first item pairs with start, the second with start+1.
- Unpack in the loop header's order: for i, item β swapping them swaps every printed pair.
- Remember start affects labels only: len(items) iterations happen regardless.
Common wrong fixes
- Expecting start=1 to skip the first element β it renumbers, it does not slice.
- Unpacking as (item, i) while the header says (i, item), transposing all output.
- Using enumerate without start and manually printing i+1 in some lines but not others, mixing 0-based and 1-based labels.
Rule to remember
enumerate's start shifts the counter, nothing else. The nth item always pairs with start + n - 1, and every item still appears exactly once.
π Learning path: Loops
Problem 13 of 16 in this path. Work them in order β each one builds on the rule before it.
β for loop with range and stepNext in path: Loop variable after the loop ends β
More loops problems
Keep going
- Output-prediction strategies β how to predict tricky snippets without running them.
- Browse every loops problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.