Ask Your Question

Revision history [back]

As @John Palmieri suggests, use iterators instead of lists.

You are already importing product from the itertools module, learn more about it, especially izip to replace zip.

Here is how to use iterators instead of lists.

from itertools import product
from itertools import izip

A = range(3)
B = range(2)

S = (izip(B,i) for i in product(A, repeat=len(B)))

As @John Palmieri suggests, use iterators instead of lists.

You are already importing product from the itertools module, learn more about it, especially izip to replace zip.

Here is how to use iterators instead of lists.

from itertools import product
from itertools import izip

A = range(3)
B = range(2)

S = (izip(B,i) for i in product(A, repeat=len(B)))

To compare this code with the code quoted in the question,

  • S is now an iterator instead of a list. This is achieved by doing S = (... for ... in ...).
  • each elementof S is now an iterator instead of a list. This is achieved by using izip instead of zip.

A good resource for learning about iterators is the SageMath thematic tutorial on comprehensions, iterators ind iterables.