PCEP Computer Programming and Python Fundamentals Practice Question
A Python developer is creating a function that processes a list of dictionaries and needs to ensure the original list remains unchanged. They write the following code:
def process(data):
for item in data:item['processed'] = True
return data
What is the best-practice critique of this function?
⚠ Common exam trap
The PCEP exam often tests the distinction between modifying a list's structure (e.g., append, remove) and modifying the mutable objects the list contains, leading candidates to overlook that mutating dictionary items is still a side effect on the original data.
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
✓
The function modifies the original list elements, causing side effects.
The function mutates the dictionaries in the original list by adding the key 'processed' to each one. This violates the principle of avoiding side effects in functions, as the caller's data is changed unexpectedly. In Python, dictionaries are mutable objects, so modifying them inside a function affects the original list elements.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The indentation should be 2 spaces instead of 4 to conform to PEP 8.
Why it's wrong here
PEP 8 mandates four spaces per indentation level, so two spaces would violate the standard rather than conform to it. Indentation style is purely cosmetic here and has no bearing on whether the caller's original list and its dictionaries are mutated by the in-place assignment.
- ✗
The function should use a list comprehension instead of a loop.
Why it's wrong here
A list comprehension builds a new list but still assigns into the same dictionary objects, so the caller's dictionaries are mutated regardless. Comprehensions suit transforming or filtering sequences into new collections, not this requirement to leave the caller's nested dictionaries untouched.
- ✗
The function returns a value but does not use it, which is acceptable.
Why it's wrong here
Returning the mutated list is harmless; the actual defect is that the function mutates each dictionary in place, so the caller's original data changes. Returning a value is normal Python practise, and this option would be the right critique only if the return were genuinely dead code with no side effects.
- ✓
The function modifies the original list elements, causing side effects.
Why this is correct
Mutating each dictionary in place adds a `processed` key to the caller's own objects, so the original list's elements change despite the function returning `data`. This violates the stated requirement that the original list remain unchanged. To avoid the side effect, build and return new dictionaries instead of editing the passed-in ones.
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.