This can be achieved using the function.
def gen(): for chrom in bins: for a_bin in bins[chrom]: for pos in a_bin: yield pos
You can iterate over the elements generated using gen() , although there is no "list of elements" that is built, but built on demand:
for pos in gen(): pass
It also means that if you exit the loop earlier, the gen() method will be aborted (with an exception). See corutines to see how this is implemented.
source share