PCEP Control Flow, Loops, Lists and Logic Practice Question
A data analyst writes a Python script to double each element in a matrix without altering the original. The code is:
original = [[1,2,3],[4,5,6]] copy = original
for i in range(len(original)):
for j in range(len(original[i])):copy[i][j] *= 2
print(original)
The output shows [[2,4,6],[8,10,12]], meaning the original was also changed. Which single modification to the line 'copy = original' ensures the original matrix remains unchanged?
⚠ Common exam trap
Python Institute often tests the distinction between shallow and deep copy in nested structures, and the trap here is that candidates assume `original[:]` or `list(original)` create a full independent copy, not realizing that inner lists are still 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
✓
Replace `copy = original` with `import copy; copy = copy.deepcopy(original)`
`copy.deepcopy()` creates a fully independent copy of the nested list structure. In Python, assignment (`copy = original`) only copies the reference to the outer list, so modifying elements through `copy` also modifies `original`. Shallow copies (like `original[:]` or `list(original)`) copy the outer list but still share references to the inner lists, so changes to inner elements affect both. Only `deepcopy` recursively duplicates all nested objects, ensuring the original matrix remains unchanged.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Replace `copy = original` with `copy = original[:]`
Why it's wrong here
This creates a shallow copy; inner lists are still shared, so modifications affect original.
- ✓
Replace `copy = original` with `import copy; copy = copy.deepcopy(original)`
Why this is correct
deepcopy creates independent copies of all nested objects.
- ✗
Replace `copy = original` with `copy = list(original)`
Why it's wrong here
Similar to slicing, list() creates a shallow copy.
- ✗
Replace the nested loop with a list comprehension: `copy = [[x*2 for x in row] for row in original]`
Why it's wrong here
This is a correct solution, but it replaces the entire loop, not just the copy assignment line. The question asks for a modification to that specific line.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-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 →
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 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?
hard- ✓ A.Use copy.deepcopy() from the copy module
- B.Assign the dictionary to a new variable (e.g., new_dict = original)
- C.Use copy.copy() on the original dictionary
- D.Use dict.copy() method
Why A: `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.
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.