python

🔶 Python: Generators

A generator yields values one at a time instead of storing everything in memory.

# List = all in memory at once
squares = [x**2 for x in range(10_000_000)]  # ~80MB

# Generator = computes one at a time
squares = (x**2 for x in range(10_000_000))  # ~120 bytes

Custom generator with yield:

def read_large_file(path):
    with open(path) as f:
        for line in f:
            yield line.strip()  # One line at a time

for line in read_large_file('huge.csv'):
    process(line)  # Never loads entire file

Practice Questions

Q: What's the difference between [x for x in range(n)] and (x for x in range(n))?