What’s the difference between list and dictionary comprehensions in Python?

Updated May 4, 2026

Short answer

List comprehension is used to create lists in a concise way, while dictionary comprehension is used to create dictionaries. Both provide a compact syntax for generating collections using loops and conditions.

Deep explanation

Both list and dictionary comprehensions are part of Python’s expressive syntax that allows you to write cleaner and more readable code.

🔸 List Comprehension

Syntax:

Python
[expression for item in iterable if condition]
  • Produces a list
  • Iterates over an iterable
  • Optionally filters items using a condition
  • Applies an expression to each item

Example:

Python
squares = [x**2 for x in range(5)]
# Output: [0, 1, 4, 9, 16]

---

🔸 Dictionary Comprehension

Syntax:

Python
{key_expression: value_expression for item in iterable if condition}
  • Produces a dictionary
  • Requires both key and value expressions
  • Useful for mapping relationships

Example:

Python
square_dict = {x: x**2 for x in range(5)}
# Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

---

🔸 Key Differences

FeatureList ComprehensionDictionary Comprehension
Output TypeListDictionary
Syntax[ ]{ }
StructureSingle expressionKey-value pair
Use CaseOrdered data collectionKey-value mapping

---

Real-world example

Scenario: Processing Student Scores

You have a list of student marks and want to:

✅ Using List Comprehension

Get only passing marks:

Python
marks = [45, 78, 32, 90, 55]
passing = [m for m in marks if m >= 50]
# [78, 90, 55]

---

✅ Using Dictionary Comprehension

Map student index to grades:

Python
marks = [45, 78, 32, 90, 55]
grade_map = {i: m for i, m in enumerate(marks)}
# {0: 45, 1: 78, 2: 32, 3: 90, 4: 55}

---

Common mistakes

  • ### ❌ 1. Confusing syntax
  • ```python
  • [x: x**2 for x in range(5)] # ❌ Invalid
  • ```
  • ### ❌ 2. Forgetting key-value in dict comprehension
  • ```python
  • {x**2 for x in range(5)} # ❌ This creates a set, not dict
  • ```
  • ### ❌ 3. Overcomplicating expressions
  • Avoid deeply nested comprehensions that reduce readability:
  • ```python
  • # Hard to read
  • [x*y for x in range(5) for y in range(5) if x % 2 == 0]
  • ```
  • ### ❌ 4. Ignoring readability
  • Sometimes a simple loop is clearer:
  • ```python
  • result = []
  • for x in data:
  • if condition:
  • result.append(process(x))
  • ```
  • ---

Follow-up questions

  • Can we use multiple conditions in comprehensions?
  • Which is faster: comprehension or loop?
  • Can dictionary comprehension have nested loops?
  • When should I avoid comprehensions?
  • What is similar to list/dict comprehension?

More Python interview questions

View all →