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
Slicing copies only the outer list, so both lists still reference the same inner row objects; mutating copy[i][j] therefore alters original. It is tempting because slicing works for flat lists, but nested structures need each inner list copied separately, for example with a comprehension.
- ✓
Replace `copy = original` with `import copy; copy = copy.deepcopy(original)`
Why this is correct
Plain assignment binds both names to the same nested list, so mutating copy also mutates original. copy.deepcopy recursively duplicates every inner list, giving independent objects and satisfying the requirement that the original matrix stays unchanged.
- ✗
Replace `copy = original` with `copy = list(original)`
Why it's wrong here
list(original) creates a shallow copy, so the inner row lists are still shared and mutating copy[i][j] changes original. It is tempting because list() copies flat lists correctly, but nested lists require each inner list to be copied independently, such as with a comprehension.
- ✗
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 builds a new nested list of doubled values, so original is never mutated, but it replaces the loop rather than the assignment line the question asks about. It is tempting because it produces the correct output, yet it does not modify 'copy = original' as specified.
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 →
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.