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
Indexing via range(len(users)) reproduces the same explicit append loop, adding positional bookkeeping without any concision, so it does not satisfy the senior developer's idiomatic request. It is tempting when indices are genuinely needed, such as mutating elements in place, but here only names are collected.
- ✗
minors = [user for user in users if user['age'] < 18]
Why it's wrong here
This comprehension filters users under 18 but appends the whole user dictionary, not user['name'], so minors holds dicts instead of names. It is tempting because the filter condition is correct and comprehensions are idiomatic, yet the output expression must extract the name key.
- ✗
minors = list(map(lambda u: u['name'], filter(lambda u: u['age'] < 18, users)))
Why it's wrong here
The map/filter combination is functionally correct but nests two lambdas, which is less readable than a single comprehension and arguably not the idiomatic replacement requested. It is tempting when reusing existing named functions, where map and filter avoid lambda overhead.
- ✓
minors = [user['name'] for user in users if user['age'] < 18]
Why this is correct
A list comprehension collapses the loop, condition, and append into one expression, producing the same filtered names list. It is the idiomatic Python replacement, satisfying the senior developer's request for conciseness while preserving identical behaviour and output.
Go deeper
Related to this question
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 →
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.