I have a function which returns a list of tuples, that I would like to iterate through:
def get_parameter_product(num_parameters, lower_range, upper_range):
param_lists = [ xrange(lower_range, upper_range) for _ in xrange(num_parameters)]
return list(itertools.product(*param_lists))
for p in get_parameter_product(3, 0, 5):
print p,
(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), ... , (4, 4, 2), (4, 4, 3), (4, 4, 4)
However, for larger values of num_parameters is take a lot of memory to allocate. Is it possible to convert this to a generator?
itertools.productis already a generator. You can just return it instead of converting it to a list.