List, Dict & Set Comprehensions Interview Questions & Answers
5 questions Updated 2026-06-18
Python interview questions on list, dict, and set comprehensions, filtering with if, conditional expressions, nested comprehensions, and when not to use them.
A list comprehension is a concise expression that builds a list in a
single readable line: [expression for item in iterable]. It replaces the
common pattern of creating an empty list and append-ing in a for loop.
# the verbose way
squares = []
for n in range(5):
squares.append(n * n)
# the comprehension
squares = [n * n for n in range(5)] # [0, 1, 4, 9, 16]
Beyond brevity, comprehensions are usually faster than an equivalent loop (the iteration runs in optimized C) and they signal intent: "I'm transforming a sequence into a new list." Reach for one whenever you're building a list by mapping or filtering another iterable.
A trailing if clause filters items — only elements for which it is
truthy are kept. A conditional expression (x if cond else y) goes at
the front, before the for, because it's part of the output expression,
not a filter.
nums = range(6)
evens = [n for n in nums if n % 2 == 0] # filter: [0, 2, 4]
labels = ["even" if n % 2 == 0 else "odd" # transform every item
for n in nums] # ['even','odd',...]
both = [n * 2 for n in nums if n > 2] # filter THEN transform
Remember the position rule: a filter if comes after the for; a
if/else expression comes before it. Mixing them up is a common
beginner error.
You can chain multiple for clauses to flatten or iterate over nested
data. The clauses read left to right in the same order as nested
loops. You can also nest a comprehension inside the output expression to
build a list of lists.
matrix = [[1, 2], [3, 4]]
flat = [x for row in matrix for x in row] # [1, 2, 3, 4]
# equivalent to:
# for row in matrix:
# for x in row:
grid = [[r * c for c in range(3)] for r in range(3)] # list of rows
The trap is reading the order backwards — the outer loop is written first. Keep nesting shallow; two levels is usually the readability limit before a plain loop is clearer.
The same syntax works for dicts and sets. A dict comprehension uses
{key: value for ...} and a set comprehension uses {expr for ...}
(no colon). Sets automatically deduplicate results.
names = ["ada", "grace", "ada"]
lengths = {n: len(n) for n in names} # {'ada': 3, 'grace': 5}
unique = {n for n in names} # {'ada', 'grace'} (deduped)
swapped = {v: k for k, v in lengths.items()} # invert a dict
Note {} alone is an empty dict, not a set — use set() for an empty
set. Dict and set comprehensions accept the same if filters and nested
for clauses as list comprehensions.
Comprehensions are for building a collection. Avoid them when you only
want side effects (printing, writing to a DB) — that abuse hides intent
and builds a throwaway list. Use a plain for loop instead. Also skip them
when the logic is so complex that the line becomes unreadable.
# bad: comprehension purely for side effects
[print(x) for x in items] # builds a useless list of Nones
# good: a loop says "do this for each"
for x in items:
print(x)
Comprehensions overlap with map/filter, but are usually more readable
and avoid lambda. Prefer a generator expression (...) over a list comp
when you only iterate once and don't need to materialize the whole result.
Practice tests are coming soon
Get notified when interactive mock interviews and quizzes launch.