Comprehensions and Iterators Flashcards
6 cards from real PCAP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 6 Comprehensions and Iterators flashcards as text
Which syntax creates a list comprehension in Python?
Answer: [x for x in range(5)]
List comprehensions use square brackets: `[expression for item in iterable]`.
What does a generator expression return compared to a list comprehension?
Answer: A lazy iterator evaluated on demand
A generator expression (using parentheses) returns an iterator that yields values lazily, using less memory.
What is the result of `[x**2 for x in range(4)]`?
Answer: [0, 1, 4, 9]
`range(4)` produces 0,1,2,3 and squaring each gives [0, 1, 4, 9].
How do you add a condition to a list comprehension to include only even numbers?
Answer: [x for x in range(10) if x%2==0]
A conditional list comprehension uses `if condition` at the end: `[x for x in iterable if condition]`.
Which syntax creates a dictionary comprehension?
Answer: {k:v for k,v in items}
Dictionary comprehensions use `{key: value for ...}` syntax with curly braces and a colon.
What does the `enumerate()` function return when iterating?
Answer: A tuple of (index, value)
`enumerate()` yields `(index, value)` tuples for each item in the iterable.
How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. Exam facts are checked against the official sources listed below. Not reviewed by a subject-matter expert. Editorial policy
Sources & references
Exam facts on this page (fees, format, dates, passing scores) are checked against these official sources; the date is our last check. Editorial policy
- pythoninstitute.org — Eligibility, fees, retake fee, retake policy, score release, time limit, score validity · verified Nov 2026
- home.pearsonvue.com — Test delivery · verified Nov 2026