PCEP Control Flow, Loops, Lists and Logic Practice Question
A Python programmer is working with a list of strings called words. The programmer wants to create a new list that contains the lengths of each word, but only for words that have more than three characters. Which code snippet correctly produces this list?
⚠ Common exam trap
The trap here is comparing a string to an integer, which is invalid, or forgetting to apply the length transformation.
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
✓
lengths = [len(word) for word in words if len(word) > 3]
The correct snippet must filter words with more than three characters and then transform each remaining word into its length. The list comprehension that uses len(word) in the expression and filters with len(word) > 3 accomplishes both steps correctly. Other options either compare the wrong types, return the wrong data, or incorrectly append both words and lengths.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
lengths = [len(word) for word in words if len(word) > 3]
Why this is correct
This list comprehension iterates over each word in words, checks if the length of the word is greater than 3, and if so includes len(word) in the new list. It correctly filters and transforms the data in a single concise expression.
- ✗
lengths = [] for word in words: if len(word) > 3: lengths.append(word) lengths.append(len(word))
Why it's wrong here
This loop appends the word itself when the condition is true, and then unconditionally appends the length of the word regardless of the condition. This results in a list containing both words and lengths, and includes lengths for words that do not meet the criteria, which is incorrect.
- ✗
lengths = [word for word in words if len(word) > 3]
Why it's wrong here
This list comprehension includes the word itself, not its length, for words longer than three characters. The requirement is to produce a list of lengths, so this returns the wrong data type and does not satisfy the transformation step.
- ✗
lengths = [len(word) for word in words if word > 3]
Why it's wrong here
This snippet compares the word itself to the integer 3, which is a type mismatch in Python 3 and will raise a TypeError. It does not check the length of the word, so it fails to filter correctly and will cause an error at runtime.
Go deeper
Related to this question
About these practice questions
One of 482 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 and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Python Institute exam blueprint
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.