Introduction to Comprehensions
Comprehensions in Python are a concise and readable way to create lists,
dictionaries, and sets. They provide a more compact syntax for generating new
sequences from existing iterables, often replacing the need for verbose for loops. This
functional programming feature leads to code that is both elegant and efficient.
This guide covers:
● List Comprehensions
● Dictionary Comprehensions
● Set Comprehensions
● Generator Expressions
1. List Comprehensions
List comprehensions offer a short and elegant syntax for creating a new list based on
the values of an existing iterable. They are often more readable and performant than
using a for loop.
Basic Syntax
The simplest form of a list comprehension is:
[expression for item in iterable]
This is equivalent to the following for loop:
new_list = []
for item in iterable:
new_list.append(expression)
Example: Squaring numbers
# Using a list comprehension
squares = [x**2 for x in range(1, 6)]
print(squares) # Output: [1, 4, 9, 16, 25]
# Equivalent for loop
squares_loop = []
for x in range(1, 6):
squares_loop.append(x**2)
print(squares_loop) # Output: [1, 4, 9, 16, 25]
Syntax with Conditional Logic
You can add a condition to filter items from the original iterable.
Syntax:
[expression for item in iterable if condition]
This is equivalent to:
new_list = []
for item in iterable:
if condition:
new_list.append(expression)
Example: Getting only even squares
even_squares = [x**2 for x in range(1, 11) if x % 2 == 0]
print(even_squares) # Output: [4, 16, 36, 64, 100]
Syntax with if-else
You can also use an if-else clause on the expression part to perform a transformation
based on a condition for every item.
Syntax:
[expression_if_true if condition else expression_if_false for item in iterable]
Example: Categorize numbers as 'even' or 'odd'
numbers = [1, 2, 3, 4, 5]
categories = ['even' if num % 2 == 0 else 'odd' for num in numbers]
print(categories) # Output: ['odd', 'even', 'odd', 'even', 'odd']
2. Dictionary Comprehensions
Similar to list comprehensions, dictionary comprehensions provide a concise way to
create dictionaries.
Basic Syntax
{key_expression: value_expression for item in iterable}
Example: Creating a dictionary of squares
# Create a dict where key is number and value is its square
square_dict = {x: x**2 for x in range(1, 6)}
print(square_dict) # Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Syntax with Conditional Logic
You can add a condition to filter which key-value pairs are included in the new
dictionary.
Syntax:
{key_expression: value_expression for item in iterable if condition}
Example: Creating a dictionary of squares for odd numbers only
odd_square_dict = {x: x**2 for x in range(1, 11) if x % 2 != 0}
print(odd_square_dict) # Output: {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
Real-world Example: Swapping keys and values
original_dict = {'a': 1, 'b': 2, 'c': 3}
swapped_dict = {value: key for key, value in original_dict.items()}
print(swapped_dict) # Output: {1: 'a', 2: 'b', 3: 'c'}
3. Set Comprehensions
Set comprehensions are very similar to list comprehensions, but they create a set
instead of a list. The key difference is that sets only store unique elements.
Basic Syntax
{expression for item in iterable}
Example: Creating a set of unique squared numbers
numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
unique_squares_set = {x**2 for x in numbers}
print(unique_squares_set) # Output: {1, 4, 9, 16}
Notice how the duplicate values in the original numbers list are automatically handled.
Syntax with Conditional Logic
Like other comprehensions, you can add a filtering condition.
Example: Creating a set of unique vowels from a string
sentence = "the quick brown fox jumps over the lazy dog"
vowels = {char for char in sentence if char in 'aeiou'}
print(vowels) # Output: {'o', 'u', 'e', 'a', 'i'} (order may vary)
4. Generator Expressions
A generator expression looks almost identical to a list comprehension, but it uses
parentheses () instead of square brackets []. The fundamental difference is how they
produce their results.
● List Comprehensions create the entire list in memory at once (eager evaluation).
● Generator Expressions create a generator object. This object produces items
one by one as they are needed, without storing them all in memory (lazy
evaluation).
Syntax
(expression for item in iterable if condition)
Key Features
● Memory Efficiency: Because they produce items one at a time, generator
expressions are highly memory-efficient and are the preferred choice when
working with very large datasets or infinite sequences.
● Lazy Evaluation: The values are generated on-the-fly, as you iterate over the
generator.
Example: Comparing memory usage
import sys
# List comprehension (eager evaluation)
list_comp = [i for i in range(10000)]
print(f"List comprehension size: {sys.getsizeof(list_comp)} bytes")
# Generator expression (lazy evaluation)
gen_exp = (i for i in range(10000))
print(f"Generator expression size: {sys.getsizeof(gen_exp)} bytes")
# To get the values from a generator, you must iterate over it
# For example, converting it to a list:
# generated_list = list(gen_exp)
The generator object itself is very small, regardless of the number of items it can
produce. The list comprehension, however, grows in size with the number of items.
When to Use Generator Expressions
Use a generator expression when:
1. You are working with a very large dataset that might not fit into memory.
2. You don't need to have all the results at once and will only be iterating through
them a single time.
3. You are passing the sequence to another function that will consume it, like sum(),
max(), or a for loop.
Example: Summing squares of a large range of numbers
# This would create a massive list in memory if it were a list comprehension
sum_of_squares = sum(x**2 for x in range(1000000))
print(f"Sum of squares: {sum_of_squares}")
Comparison Summary
Feature List Dictionary Set Generator
Comprehensio Comprehensio Comprehensio Expression
n n n
Syntax [... for ... in ...] {... : ... for ... in {... for ... in ...} (... for ... in ...)
...}
Result Type list dict set generator object
Evaluation Eager (creates Eager (creates Eager (creates Lazy (yields
full list) full dict) full set) items on
demand)
Memory Use Proportional to Proportional to Proportional to Constant and
number of items number of items unique items small
Use Case Creating lists Creating Creating sets Memory-efficien
from other dictionaries (unique items). t iteration, large
iterables. from iterables. data.
Duplicates Preserves Keys must be Removes Yields all items
duplicates unique duplicates (if in iterable)
(overwrites old)