PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
Given a list of names = ['Alice', 'Bob', 'Charlie'], a developer wants to create a dictionary mapping each name to its length. Which expression accomplishes this?
⚠ Common exam trap
Many exam-takers confuse the key-value order in a dictionary comprehension or forget to call `len()` as a function, leading them to pick options that either swap the mapping or use an undefined variable.
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
✓
{name: len(name) for name in names}
It uses a dictionary comprehension that iterates over each name in the list, using the name as the key and the result of `len(name)` as the value. This directly maps each name to its length, which is exactly what the developer wants.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
{len(name): name for name in names}
Why it's wrong here
This inverts the mapping, placing each name's length as the key and the name as the value, which reverses the required direction. It is tempting because both len(name) and name appear correctly, and would be the right expression if the task were mapping lengths back to names.
- ✓
{name: len(name) for name in names}
Why this is correct
The dictionary comprehension iterates over each element in `names`, binding `name` to the string and computing `len(name)` as its value, producing `{'Alice': 5, 'Bob': 3, 'Charlie': 7}`. This satisfies the requirement to map every name to its length in a single expression, unlike `map` or `zip` alternatives.
- ✗
{name: len for name in names}
Why it's wrong here
Using the built-in len function object as the value stores a reference to len itself, not the computed length, so every key maps to the same function. It is tempting because len appears in the expression, and would be correct if written as len(name) to invoke the function per element.
- ✗
{name: length for name in names}
Why it's wrong here
The comprehension references the undefined name 'length', raising NameError rather than calling len(name). It is tempting because the syntax mirrors a valid dictionary comprehension shape, and would be correct if 'length' were a previously defined variable or function returning each name's length.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 482-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.