Navigation

Python

How to Use List Comprehensions vs Generator Expressions

Choose between list comprehensions and generator expressions in Python. Memory-efficient solutions for different use cases and performance optimization

Table Of Contents

Problem

You need to process sequences efficiently but don't know when to use list comprehensions [] versus generator expressions ().

Solution

# List comprehension - creates entire list in memory
numbers = [1, 2, 3, 4, 5]
squares_list = [x**2 for x in numbers]
print(squares_list)  # [1, 4, 9, 16, 25]

# Generator expression - creates iterator, lazy evaluation
squares_gen = (x**2 for x in numbers)
print(list(squares_gen))  # [1, 4, 9, 16, 25]

# Memory comparison
import sys
list_comp = [x for x in range(1000)]
gen_exp = (x for x in range(1000))
print(sys.getsizeof(list_comp))  # ~9000 bytes
print(sys.getsizeof(gen_exp))    # ~120 bytes

# Use list comprehension when you need the full list
filtered_data = [x for x in range(100) if x % 2 == 0]
print(len(filtered_data))  # Need immediate access to all items

# Use generator when processing large datasets
large_gen = (x**2 for x in range(1000000))
for item in large_gen:
    if item > 1000:
        break  # Only compute what's needed

# Generator for file processing
def read_lines():
    with open('large_file.txt') as f:
        return (line.strip().upper() for line in f)

# Only loads one line at a time

Explanation

List comprehensions create the entire list in memory immediately. Generator expressions create an iterator that computes values on-demand, using less memory.

Use list comprehensions when you need immediate access to all elements. Use generators for large datasets or when you only need to iterate once.

Share this article

Add Comment

No comments yet. Be the first to comment!

More from Python