PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A function receives a dictionary that may contain nested dictionaries. The function must modify the dictionary without affecting the original passed argument. Which technique ensures a complete independent copy?
⚠ Common exam trap
The PCEP exam often tests the distinction between shallow and deep copies, and the trap here is that candidates assume `dict.copy()` or `copy.copy()` create a full independent copy, overlooking that nested dictionaries remain shared references.
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 copy.deepcopy() from the copy module
`copy.deepcopy()` recursively copies all objects within the dictionary, including nested dictionaries, creating a completely independent copy. This ensures modifications to the copy do not affect the original argument, which is required when the dictionary contains mutable nested structures.
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 copy.deepcopy() from the copy module
Why this is correct
copy.deepcopy() recursively duplicates every nested dictionary and list, producing a fully independent object graph. A shallow copy or dict() only copies the top level, leaving nested references shared, so mutations would still propagate to the original argument.
- ✗
Assign the dictionary to a new variable (e.g., new_dict = original)
Why it's wrong here
Assignment binds a second name to the same dict object, so mutations through either reference affect both; nested values are shared too. It is tempting because it looks like duplication, and it is correct when you genuinely want an alias to the same data rather than an independent copy.
- ✗
Use copy.copy() on the original dictionary
Why it's wrong here
copy.copy() performs a shallow copy: the top-level dict is new, but nested dicts remain shared, so modifying them alters the original. It is tempting because it does create a distinct object. copy.deepcopy() recurses through every nested level, which is what this scenario requires.
- ✗
Use dict.copy() method
Why it's wrong here
dict.copy() also produces a shallow copy, leaving nested dictionaries bound to the original, so nested edits propagate back. It is tempting because it is the idiomatic way to duplicate a flat dict. copy.deepcopy() recursively copies each nested level, satisfying the independence requirement here.
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.