📡 You're offline — showing cached content
New version available!
Quick Access
Python Beginner

Python List Comprehensions and Generator Expressions: Complete Guide

Master Python list comprehensions, dict/set comprehensions, generator expressions — syntax, nested comprehensions, and when to use each.

EzyCoders Admin January 29, 2026 10 min read 1 views
Python List Comprehensions Generator Guide
Share: Twitter LinkedIn WhatsApp

List Comprehensions

List comprehensions are Python's most idiomatic way to create lists. They are more readable than loops, often faster, and demonstrate Python fluency that interviewers look for. Master them along with dict, set, and generator expressions.

Basic Syntax

# Syntax: [expression for item in iterable if condition]

# Instead of a loop:
squares = []
for x in range(10):
    squares.append(x ** 2)

# One line with comprehension:
squares = [x ** 2 for x in range(10)]
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# With condition (filter)
even_squares = [x**2 for x in range(10) if x % 2 == 0]
# [0, 4, 16, 36, 64]

# Transform strings
names = ['rahul', 'priya', 'amit', 'neha']
title = [name.capitalize() for name in names]
# ['Rahul', 'Priya', 'Amit', 'Neha']

Nested Comprehensions

# Matrix multiplication (nested loops)
matrix = [[1,2,3],[4,5,6],[7,8,9]]

# Flatten 2D matrix to 1D
flat = [num for row in matrix for num in row]
# [1,2,3,4,5,6,7,8,9]

# Transpose matrix
transposed = [[row[i] for row in matrix] for i in range(3)]
# [[1,4,7],[2,5,8],[3,6,9]]

# Cartesian product
colors = ['red', 'blue']
sizes  = ['S', 'M', 'L']
combos = [(c, s) for c in colors for s in sizes]
# [('red','S'),('red','M'),('red','L'),('blue','S'),...]

Dict and Set Comprehensions

# Dict comprehension
squares_dict = {x: x**2 for x in range(6)}
# {0:0, 1:1, 2:4, 3:9, 4:16, 5:25}

# Invert a dictionary
original = {'a': 1, 'b': 2, 'c': 3}
inverted = {v: k for k, v in original.items()}
# {1:'a', 2:'b', 3:'c'}

# Filter dict entries
scores = {'Rahul':85, 'Priya':92, 'Amit':74, 'Neha':95}
top    = {name: score for name, score in scores.items() if score >= 90}
# {'Priya':92, 'Neha':95}

# Set comprehension — unique values only
nums    = [1, 2, 2, 3, 3, 3, 4]
unique  = {x**2 for x in nums}
# {1, 4, 9, 16}

Generator Expressions — Lazy Evaluation

# Generator: () instead of [] — evaluates lazily (one at a time)
# Huge memory saving for large datasets

# List: creates all 10 million items in memory immediately
squares_list = [x**2 for x in range(10_000_000)]  # ~400 MB

# Generator: produces values one by one on demand
squares_gen  = (x**2 for x in range(10_000_000))   # ~200 bytes!

# Consume with next() or iteration
print(next(squares_gen))  # 0
print(next(squares_gen))  # 1

# Sum without storing all values
total = sum(x**2 for x in range(10_000_000))  # memory efficient

# Chain generators
import os
py_files = (f for f in os.listdir('.') if f.endswith('.py'))
contents = (open(f).read() for f in py_files)
lines    = (line for content in contents for line in content.split('\n'))

Real-World Examples

# Parse CSV-like data
csv = "Rahul,25,Mumbai\nPriya,30,Delhi\nAmit,22,Pune"
users = [
    {'name': row[0], 'age': int(row[1]), 'city': row[2]}
    for row in (line.split(',') for line in csv.strip().split('\n'))
]

# Extract unique tags from posts
posts = [
    {'title': 'PHP Guide', 'tags': ['php', 'backend', 'interview']},
    {'title': 'JS Closures','tags': ['javascript', 'interview', 'frontend']},
]
all_tags    = [tag for post in posts for tag in post['tags']]
unique_tags = list({tag for post in posts for tag in post['tags']})
# ['php', 'backend', 'interview', 'javascript', 'frontend']

Q: When should you NOT use a list comprehension?

When it becomes too complex to read in one line — a regular loop is clearer. When you only need to iterate once — use a generator expression instead to save memory. When the logic requires multiple steps — break it into named variables.


Q: What is the difference between a list comprehension and a generator expression?

A list comprehension [x for x in ...] creates the entire list in memory immediately. A generator expression (x for x in ...) is lazy — it produces values one at a time only when requested. Generators use O(1) memory regardless of the sequence length.

EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment