Question 33 of 498
PCEP Control Flow, Loops, Lists and Logic Practice Question
A list contains strings and numbers: items = ['apple', 10, 'banana', 20]. A programmer wants to create a new list that contains only the strings. Which approach is correct?
⚠ Common exam trap
Python Institute often tests the distinction between comparing a type object to a string literal (e.g., `type(item) is 'str'`) versus comparing to the actual type object (e.g., `type(item) == str`), trapping candidates who mistakenly think `'str'` is the same as `str`.
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
✓
[item for item in items if isinstance(item, str)]
The `isinstance(item, str)` function is the recommended way to check if an item is a string because it handles inheritance and is more readable. Option C, `type(item) == str`, also works in this case but is not considered the best practice for type checking in Python, especially when dealing with subclasses. Option A uses `isdigit()`, which only works for strings that represent digits, not all strings. Option B uses `type(item) is 'str'`, which compares the type to a string literal, which is incorrect.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
[item for item in items if item.isdigit()]
Why it's wrong here
Incorrect because `isdigit()` is a string method that returns True only if the string consists of digits. It is not suitable for filtering all strings.
- ✗
[item for item in items if type(item) is 'str']
Why it's wrong here
Incorrect because `type(item) is 'str'` compares the type object to a string literal, which is not valid. The correct comparison would be `type(item) is str`.
- ✗
[item for item in items if type(item) == str]
Why it's wrong here
Incorrect because although `type(item) == str` works for basic strings, it is not the recommended approach; `isinstance` is preferred for type checking in Python.
- ✓
[item for item in items if isinstance(item, str)]
Why this is correct
Correct. The `isinstance()` function checks if an item is an instance of a specified class and is the standard way to test for type.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jun 30, 2026
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.