CertGrid CertGrid
Concepts·Certified Associate Python Programmer

Python List Comprehensions

PCAP objective 5.1 is build complex lists using list comprehensions. The syntax is an expression, a `for`, and an optional `if`, and once you can read those three parts in order the rest is practice. The examinable details are that there are four kinds of comprehension distinguished only by their brackets, that the loop variable does not survive the comprehension, and that a comprehension is the right fix for the `[[0] * 2] * 3` trap.

Comprehensions and Lambdas Guide 20 of 25 Intermediate

Written against the versions above. Comprehension variables have been scoped to the comprehension since **Python 3.0** - in Python 2 they leaked into the enclosing scope. That is one of the differences on this page and older material gets it wrong.

One machine, and any shell with Python 3 will do - these exams test the language, not a distribution.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSPython 3.14.4 - the only machine this path needs2 Core4 GB50 GB

Before you start

  1. The loop, and the comprehension that replaces it

  2. The three parts, and both kinds of if

  3. The other three comprehension types

  4. The grid, built properly this time

  5. The loop variable does not leak

  6. Comprehensions in the built-ins that consume them

  7. What the exam does with this objective

Official sources