Python Functions
Python Generators
A generator is a special kind of function that produces values one at a time, on demand, instead of building and storing an entire list in memory at once — making it far more memory efficient for large sequences.
The yield Keyword
A generator function looks like a normal function but uses yield instead of return. Each time yield runs, the function pauses, hands back a value, and remembers exactly where it left off, ready to continue from that point the next time a value is requested.
def count_up_to(n):
i = 1
while i <= n:
yield i
i += 1
for number in count_up_to(5):
print(number)
# 1 2 3 4 5, one at a timeLazy Evaluation
Generators are "lazy" — they compute the next value only when it is actually asked for, instead of computing every value up front. Calling a generator function does not run its body at all; it just creates a generator object.
gen = count_up_to(3)
print(gen) # <generator object ...>
print(next(gen)) # 1
print(next(gen)) # 2
print(next(gen)) # 3
# next(gen) again -> raises StopIterationGenerator Expressions
A generator expression looks just like a list comprehension but uses parentheses () instead of square brackets []. It builds a generator instead of a full list, which is a quick way to get laziness without writing a whole function.
squares_list = [x * x for x in range(1000000)] # builds full list in memory
squares_gen = (x * x for x in range(1000000)) # builds nothing yet
print(next(squares_gen)) # 0
print(next(squares_gen)) # 1Memory Efficiency vs Lists
A list stores every single element in memory at once. A generator stores only its current position and the logic to produce the next value, so it uses a tiny, constant amount of memory no matter how large the sequence is — ideal for reading huge files or streams of data.
| Aspect | List | Generator |
|---|---|---|
| Memory usage | Stores all items at once | Stores one item at a time |
| Speed to create | Slower for huge sequences | Instant — nothing computed yet |
| Re-usable? | Yes, can loop many times | No, exhausted after one pass |
| Indexing (data[2]) | Supported | Not supported |
Use a generator when you only need to loop through data once, especially for very large datasets — use a list when you need to access items by index or loop over the same data multiple times.
