Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A data analyst maintains `matrix = [[1, 2], [3, 4], [5, 6]]` and wants a flat list `flat` containing every number in row order, that is `[1, 2, 3, 4, 5, 6]`. Which code produces this result?

⚠ Common exam trap

The trap here is appending the inner list itself instead of iterating it, which keeps the data nested.

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

✓

flat = [] for row in matrix: for value in row: flat.append(value)

Flattening a list of lists requires iterating the outer list to obtain each row and then iterating each row to reach its elements. Appending every visited integer builds the one-dimensional result in the original order. Appending whole rows keeps the nesting, hard-coded concatenation does not adapt to different sizes, and selecting a single column index drops data.

Answer analysis

Option-by-option breakdown

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

  • ✗

    flat = [] for row in matrix: flat.append(row)

    Why it's wrong here

    This appends each inner list as a single element, producing `[[1, 2], [3, 4], [5, 6]]`, which is just a shallow copy of the original structure. The numbers remain nested rather than flattened. It fails because the inner elements are never iterated, so the requirement for a one-dimensional list of numbers is not met.

  • ✓

    flat = [] for row in matrix: for value in row: flat.append(value)

    Why this is correct

    The nested loops first take each inner list as `row`, then iterate that row to visit every integer, appending each to `flat`. Processing rows in order and values within each row in order yields exactly `[1, 2, 3, 4, 5, 6]`. This is the standard, explicit way to flatten a two-dimensional list and works for rows of differing lengths as well.

  • ✗

    flat = matrix[0] + matrix[1] + matrix[2]

    Why it's wrong here

    Concatenating the three inner lists does produce `[1, 2, 3, 4, 5, 6]` for this exact data, but the code is hard-coded to three rows. If the matrix gains or loses a row, the expression raises an `IndexError` or silently omits data. It is brittle and does not generalise, so it is not the correct approach for a reusable script.

  • ✗

    flat = [] for i in range(len(matrix)): flat.append(matrix[i][0])

    Why it's wrong here

    Indexing with `[0]` retrieves only the first element of each inner list, so `flat` becomes `[1, 3, 5]`. The second column is never visited, and the result omits half the data. Although the loop structure is valid, the fixed inner index makes it incorrect for producing a complete flattened list.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.