Better HN
topnewbestaskshowjobs
Log inSign up

wfitz

2 karma · joined Apr 18, 2023 (3y ago) · 1 submissions

Submissions

comment 2y ago · link · parent

Don't forget you can chain multiple generators together too. This can have huge memory and compute time benefits with lazy evaluation depending how you structure your code. A very crude example below...

# Really big generator that would probably crash as a list

divmods = (divmod(x,5) for x in range(100000000000000000000000000000000))

# Create a generator from a generator

remains = (remainder for n, remainder in divmods)

# Runs instantly

next(remains)