What Are Generators in Python?
Generators are a special type of iterable that generate items one at a time, on the fly, using lazy evaluation. They do not store the entire sequence in memory, making them ideal for large datasets or infinite sequences.
Why Use Generators?
Feature | Generators |
---|---|
Memory usage | Very low (lazy-loaded) |
Performance | Efficient iteration |
Use case | Large/infinite data |
Syntax | Uses yield or generator expressions |
Basic Generator Example
def my_generator():
for i in range(5):
yield i
gen = my_generator()
for num in gen:
print(num)
Output:
0
1
2
3
4
Each value is produced one at a time, not stored in memory.
Comparison with List
Using List (Consumes Memory)
nums = [i for i in range(1_000_000)]
Using Generator (Efficient)
nums = (i for i in range(1_000_000))
Generator expression uses constant memory, no matter how large.
Practical Use Case: Reading Large Files
def read_large_file(file_path):
with open(file_path) as f:
for line in f:
yield line.strip()
Instead of loading the whole file, it reads line-by-line, saving memory.
Generator Functions vs Generator Expressions
Generator Function (with yield
)
def count_up_to(n):
i = 0
while i < n:
yield i
i += 1
Generator Expression
gen = (i for i in range(10))
Both create lazy iterators, but the syntax differs.
How Much Memory Can You Save?
import sys
l = [i for i in range(1000000)]
g = (i for i in range(1000000))
print(sys.getsizeof(l)) # Large memory size
print(sys.getsizeof(g)) # Small (fixed) memory size
Tips for Using Generators
- Use generators when you don’t need all results at once
- Avoid converting generator to list unless needed
-
Use with
for
loops,next()
, orsum()
directly
Bonus: Infinite Generator
def infinite_counter():
i = 0
while True:
yield i
i += 1
Useful in streaming or real-time data scenarios.
Summary Table
Feature | Generator | List |
---|---|---|
Memory usage | Minimal | High (stores all items) |
Lazy evaluation | Yes | No |
Infinite sequence | Possible | Not practical |
Syntax | yield , () |
[] , list() |
0 Comments:
Post a Comment