← Back

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

  1. Write the items in order, untouched by start β€” enumerate never skips or reorders.
  2. Attach counters beginning at start: the first item pairs with start, the second with start+1.
  3. Unpack in the loop header's order: for i, item β€” swapping them swaps every printed pair.
  4. Remember start affects labels only: len(items) iterations happen regardless.

Common wrong fixes

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.

Solve it in the interactive editor β†’

πŸ” 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