First time here? Check out the FAQ!

Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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)))
click to hide/show revision 2
No.2 Revision

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.