PCEP Computer Programming and Python Fundamentals Practice Question
A developer needs to iterate over the indices of a list named 'items' and print each index and its corresponding value. Which loop construct is most appropriate?
⚠ Common exam trap
Python Institute often tests the distinction between iterating over values (`for val in items`) versus indices (`for i in range(len(items))`) versus both (`enumerate`), and the trap here is that candidates may choose Option B because it works, missing that `enumerate` is the idiomatic and recommended construct for this exact use case.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
for i, val in enumerate(items): print(i, val)
`enumerate(items)` returns an iterator that yields pairs of (index, value) directly, making it the most Pythonic and efficient way to iterate over both indices and values of a list. It avoids the overhead of calling `items.index(val)` (which is O(n) per iteration) or manually managing `range(len(items))`.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
for val in items: print(items.index(val), val)
Why it's wrong here
Using items.index(val) returns the first matching index, so duplicate values print the wrong index and repeated lookups run in linear time. It is tempting because iterating values directly is idiomatic when indices are irrelevant; here the stem explicitly requires each element's true positional index.
- ✗
for i in range(len(items)): print(i, items[i])
Why it's wrong here
range(len(items)) with indexing works, but enumerate(items) yields index and value together, avoiding manual indexing. It is tempting because range with len is the classic idiom taught for index-based iteration, and it would be correct when you need to modify items in place by index.
- ✓
for i, val in enumerate(items): print(i, val)
Why this is correct
enumerate() yields index-value pairs directly, avoiding manual counter maintenance or range(len(items)) indexing. It satisfies the requirement to print both the index and its corresponding value in one clean iteration, which is the idiomatic and most readable construct for this task.
- ✗
for i in items: print(i)
Why it's wrong here
Iterating with 'for i in items' binds i to each element, not its index, so print(i) outputs values only and never the positions the stem demands. It is tempting as the idiomatic Python loop for reading elements, which would be right if indices were not required.
Go deeper
Related to this question
About these practice questions
One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.