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 recommends 4 spaces, not 2.
- ✗
The function should use a list comprehension instead of a loop.
Why it's wrong here
A list comprehension would not avoid side effects if it mutates the dictionaries.
- ✗
The function returns a value but does not use it, which is acceptable.
Why it's wrong here
Returning a value is fine, but the function is impure due to mutation.
- ✓
The function modifies the original list elements, causing side effects.
Why this is correct
Mutating the input data violates the principle of avoiding side effects.
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 →
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.