Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A programmer is writing a Python function that processes a list of integers. The function must remove all negative numbers from the list while preserving the order of the remaining elements, and it must do so in place (modifying the original list). Which two code snippets correctly achieve this? (Choose two.)

⚠ Common exam trap

The trap here is assuming that iterating with for and removing elements is safe, when it actually skips elements due to shifting indices.

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

✓

i = 0 while i < len(numbers): if numbers[i] < 0: del numbers[i] else: i += 1

Both the while-loop with manual index management and the slice assignment using a list comprehension correctly modify the original list in place. The while loop carefully avoids skipping elements after deletion, and the slice assignment replaces the contents of the same list object. The other options either create a new list, cause iteration issues, or raise errors.

Answer analysis

Option-by-option breakdown

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

  • ✓

    i = 0 while i < len(numbers): if numbers[i] < 0: del numbers[i] else: i += 1

    Why this is correct

    This while loop uses an index i. When a negative number is found, it deletes the element at i and does not increment i, so the next element shifts into the same index and is checked. When a non-negative number is found, i increments. This correctly removes all negatives in place while preserving order.

  • ✓

    numbers[:] = [x for x in numbers if x >= 0]

    Why this is correct

    The slice assignment numbers[:] = ... replaces the entire contents of the list with the new list of non-negative numbers. This modifies the original list object in place, so any other references to the list see the change. It preserves order and removes all negatives. This is a concise and correct in-place solution.

  • ✗

    for num in numbers: if num < 0: numbers.remove(num)

    Why it's wrong here

    Iterating over a list while removing elements causes the iterator to skip elements because indices shift. This can leave some negative numbers unremoved. Additionally, remove() removes the first occurrence of the value, which may not be the current one if duplicates exist. This approach is unreliable for removing all negatives in place.

  • ✗

    for i in range(len(numbers)): if numbers[i] < 0: del numbers[i]

    Why it's wrong here

    Using a for loop over range(len(numbers)) while deleting elements causes index out-of-range errors or skips elements because the list shrinks during iteration. The range is computed once, so indices become invalid. This does not correctly remove all negatives and may raise an IndexError.

  • ✗

    numbers = [x for x in numbers if x >= 0]

    Why it's wrong here

    This list comprehension creates a new list and rebinds the local name numbers to it. The original list object is not modified, so if other references exist, they still see the old list with negatives. The requirement is to modify the list in place, so this does not satisfy the condition.

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.