PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A network configuration tool stores device settings in a dictionary where each setting key may have multiple values from different configuration sources. For example, the key 'dns_servers' might have values from the DHCP server and manual configuration. The current implementation simply assigns values: settings[key] = value. If the same key appears multiple times, only the last value is kept, losing previous values. The developer must modify the data structure so that all values for a key are preserved. The solution should be efficient for both adding new values and accessing all values for a key. Which modification is best?
⚠ Common exam trap
It's easy for candidates to think Option C is sufficient on its own, overlooking that `defaultdict` provides automatic initialization, which is the key efficiency improvement tested in the PCEP exam.
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
✓
Use a dictionary of lists with a default factory (e.g., collections.defaultdict(list))
`collections.defaultdict(list)` automatically creates a new list for each new key, allowing multiple values to be appended without overwriting. This preserves all values for a key while providing O(1) average-time access to the list of values, meeting the efficiency requirement for both adding and retrieving.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use a set for each value to avoid duplicates
Why it's wrong here
Using a set for each value would avoid duplicates but does not preserve order and would require initialization for each new key, making it less efficient for adding values.
- ✓
Use a dictionary of lists with a default factory (e.g., collections.defaultdict(list))
Why this is correct
Using a dictionary of lists with a default factory (e.g., collections.defaultdict(list)) automatically creates a new list for each new key, allowing multiple values to be appended without overwriting. This preserves all values for a key efficiently for both adding and retrieving.
- ✗
Use a list for each value, and append new values to the list
Why it's wrong here
Using a list for each value and appending works but requires manually initializing a new list for each new key, which is less convenient and error-prone. Option B improves upon this with automatic initialization.
- ✗
Use a tuple for each value, converting to list when needed
Why it's wrong here
Using a tuple for each value is immutable, so to add a value you would need to convert to list, modify, and convert back, which is inefficient and cumbersome.
Visual reference
Go deeper
Related to this question
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 →
Same concept, more angles
1 more way this is tested on PCEP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A programmer needs to store configuration settings keyed by string, where each key maps to a list of allowed values. Which data structure is most appropriate?
medium- A.A tuple of lists where each list starts with the key.
- ✓ B.A dictionary where keys are strings and values are lists.
- C.A list of tuples where each tuple contains a key and a list of values.
- D.A set of strings representing the keys, with a separate list for values.
Why B: A dictionary in Python provides direct key-to-value mapping, making it ideal for storing configuration settings where each string key must map to a list of allowed values. Dictionaries offer O(1) average-time complexity for lookups, which is efficient for retrieving the list of values for a given key. This structure directly models the requirement without unnecessary nesting or indirection.
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.