Generator Expressions

Memory-efficient comprehensions

Interview Relevant: Performance optimization

Generator Expressions

Like list comprehensions, but use parentheses and evaluate lazily.

Code Examples

python
1squares = (x**2 for x in range(1000000))
2# Doesn't build a list in memory!
3print(next(squares))  # 0

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In