Search code examples
pythonlazy-evaluationcartesian-product

Filter Cartesian product so it fits in memory


I have a list of lists

y = [[a, b, c], ...]

I want to generate the Cartesian product between all of the lists. itertools.product in python does this but all at once so I can't hold them in memory.

I only need a subset of the output though. For arguments sake say only those that sum to 1. I need to sequentially obtain results so I can drop those that that are unneeded.

Essentially

[x for x in itertools.product(*y) if sum(x)==1]

where itertools.product does not eagerly evaluate.


Solution

  • Just replace your square brackets with ( and ): that will make your resulting expression a generator (that evaluates lazily) and not a list (that will have all of its content in memory).

    (x for x in itertools.product(*y) if sum(x)==1)