PCEP Computer Programming and Python Fundamentals Practice Question
You are a developer in a financial firm. Your team is building a Python module that performs complex calculations on large datasets. To improve performance, you are using list comprehensions and built-in functions. Your code passes all unit tests, but during integration testing, the memory usage spikes unexpectedly. The problematic area is a function that constructs a large list of intermediate results using a list comprehension that references a generator. The code is:
def process(data):
results = [expensive_transform(x) for x in data]
# further processing on results
You suspect that the list comprehension stores all results in memory at once, but you need to keep the function's output as a list for subsequent operations. What is the best solution to reduce memory without changing the function's return type?
⚠ Common exam trap
The PCEP exam often tests the distinction between eager (list comprehension) and lazy (generator expression) evaluation, and the trap here is that candidates mistakenly believe wrapping a generator in list() or using .append() reduces memory, when in fact both still materialize the full list in memory.
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
✓
Change the function to return a generator and modify all callers to handle iterables.
Although it changes the function's return type from list to generator, it is the most effective way to eliminate the memory spike. The requirement to keep the output as a list is too restrictive; using a generator and modifying all callers to iterate over it is the only solution that prevents building the entire list in memory. Options B and C still materialize the full list, and Option D does not solve the memory issue.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Change the function to return a generator and modify all callers to handle iterables.
Why this is correct
Avoids building the full list if subsequent processing can consume lazily.
- ✗
Replace the list comprehension with a generator expression wrapped in list().
Why it's wrong here
This still forces immediate evaluation into a list.
- ✗
Use a for loop with the .append() method instead of comprehension.
Why it's wrong here
Same memory consumption as comprehension.
- ✗
Increase the system's available memory via configuration.
Why it's wrong here
Not a programming solution; does not address the root cause.
Go deeper
Related to this question
About these practice questions
One of 498 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.