Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A warehouse script stores stock quantities in the list levels = [4, 0, 7, 0, 2]. A developer wants to build a new list, in_stock, containing only the quantities greater than zero, preserving the original order. Which code correctly produces [4, 7, 2]?

⚠ Common exam trap

Test-takers frequently confuse the comprehension's filtering if clause with a conditional expression, which would additionally require an else branch.

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

✓

in_stock = [q for q in levels if q > 0]

A comprehension of the form [expression for item in iterable if condition] walks the list in order and includes only items satisfying the condition. Testing q > 0 excludes the zeros while keeping 4, 7, and 2 in their original positions, and it builds a separate list rather than modifying the source.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    in_stock = [levels[q] for q in levels if q > 0]

    Why it's wrong here

    Here q is treated as an index into levels, so the code looks up positions 4, 7, and 2. Index 7 is out of range for a five-element list, raising an IndexError. Even without that error, indexing by value rather than by position is conceptually wrong for filtering the values themselves.

  • ✗

    in_stock = [q for q in levels if q >= 0]

    Why it's wrong here

    The condition q >= 0 is true for the zeros as well, so every element is kept and the result is [4, 0, 7, 0, 2]. The scenario requires excluding quantities that are not greater than zero. Using the greater-than-or-equal comparison fails to filter out the zero entries, producing the wrong list.

  • ✗

    in_stock = [q if q > 0 for q in levels]

    Why it's wrong here

    This is a syntax error. A conditional expression of the form q if q > 0 must also supply an else branch, and it belongs before the for clause in a comprehension. Python raises a SyntaxError when parsing this line, so no list is produced at all and the script cannot run.

  • ✓

    in_stock = [q for q in levels if q > 0]

    Why this is correct

    This list comprehension iterates over levels in order and keeps each quantity only when q > 0, so zeros are filtered out and 4, 7, and 2 are collected in their original sequence. The result is a brand-new list, leaving levels unchanged. It is the idiomatic single-expression way to filter a list in Python.

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 →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

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.