Courseiva
Computer Programming and Python FundamentalshardMultiple ChoiceObjective-mapped

PCEP Computer Programming and Python Fundamentals Practice Question

A junior developer is working on a script that processes user data. The script reads a CSV file into a list of dictionaries. Each dictionary represents a user with keys 'name', 'age', and 'email'. The developer needs to filter out users under 18 and store their names in a list. The current code is:

users = [{'name': 'Alice', 'age': 17, 'email': 'alice@example.com'}, {'name': 'Bob', 'age': 22, 'email': 'bob@example.com'}]

minors = []

for user in users:
    if user['age'] < 18:

minors.append(user['name'])

print(minors)

The code works, but the senior developer says it is not idiomatic and suggests a more concise solution. Which of the following approaches is the best replacement?

⚠ Common exam trap

Python Institute often tests the distinction between filtering entire objects versus extracting specific fields, so candidates may pick Option B (which filters dictionaries) instead of Option D (which extracts the 'name' field), missing the requirement to store only names.

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

minors = [user['name'] for user in users if user['age'] < 18]

It uses a list comprehension to directly extract the 'name' field from each user dictionary where the age is under 18, making the code concise and Pythonic. The original code works but is verbose; list comprehensions are the idiomatic Python approach for transforming and filtering iterables in a single readable line.

Answer analysis

Option-by-option breakdown

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

  • minors = [] for i in range(len(users)): if users[i]['age'] < 18: minors.append(users[i]['name'])

    Why it's wrong here

    Same as original but uses index, not idiomatic.

  • minors = [user for user in users if user['age'] < 18]

    Why it's wrong here

    This returns dictionaries, not names.

  • minors = list(map(lambda u: u['name'], filter(lambda u: u['age'] < 18, users)))

    Why it's wrong here

    Works but less readable than comprehension.

  • minors = [user['name'] for user in users if user['age'] < 18]

    Why this is correct

    Concise and idiomatic list comprehension.

About these practice questions

Courseiva writes every PCEP question from scratch — 498 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 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.