PCEP Control Flow, Loops, Lists and Logic Practice Question
You are writing a script to parse a log file. Each line in the log contains a timestamp and a message separated by a colon. You need to extract only the messages that contain the word 'ERROR'. The script uses a list comprehension to filter lines. However, the script crashes with a 'ValueError: not enough values to unpack' when processing some lines. The code is:
lines = ['2024-01-01 12:00:00: INFO: all good', '2024-01-01 12:01:00: ERROR: something wrong'] errors = [msg for timestamp, msg in line.split(': ') for line in lines if 'ERROR' in msg]
What is the correct fix?
⚠ Common exam trap
Python Institute often tests the order of `for` clauses in nested list comprehensions and the assumption that `split()` always returns a fixed number of parts, leading candidates to choose Option C which only reorders loops without fixing the unpacking error.
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
✓
Rewrite as: [msg for line in lines if 'ERROR' in line for msg in [line.split(': ')[1]]]
It properly separates the filtering and unpacking steps. The original code attempts to unpack each line into timestamp and msg using `line.split(': ')`, but some lines may not contain exactly two parts after splitting (e.g., if the colon is missing or there are extra colons), causing a ValueError. Option D first filters lines containing 'ERROR', then safely extracts the message part by splitting and taking index [1] inside a nested comprehension, avoiding the unpacking error.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Rewrite as: [line for line in lines if 'ERROR' in line]
Why it's wrong here
Returns the whole line, not just message.
- ✗
Change the list to a dictionary
Why it's wrong here
Unnecessary.
- ✗
Swap the order of the for clauses to: [msg for line in lines for timestamp, msg in line.split(': ') if 'ERROR' in msg]
Why it's wrong here
Still crashes because line.split(': ') returns list, not unpackable.
- ✓
Rewrite as: [msg for line in lines if 'ERROR' in line for msg in [line.split(': ')[1]]]
Why this is correct
Correct nested comprehension.
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.