nickponline
nickponline

Reputation: 25914

How do I convert a list to a generator?

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?

Upvotes: 1

Views: 298

Answers (3)

Dunes
Dunes

Reputation: 40703

Your method is adding an extraneous step that is not needed.

prod_list = get_parameter_product(3, 0, 5)

can be replaced with

prod_genr = product(range(0, 5), repeat=3)

Anything in itertools returns a generator rather than list or tuple. You may wish to look at the documentation to check you really want product as opposed to combinations or permutations.

Upvotes: 2

Tim
Tim

Reputation: 12174

itertools.product is already a generator. You can just return it instead of converting it to a list.

Upvotes: 2

abarnert
abarnert

Reputation: 365747

You don't need to convert a list to a generator—just don't convert your generators into lists.

First, to build up param_lists, use a generator expression instead of a list comprehension, and you'll have an iterator instead of a list.

Second, don't call list in the return statement.

>>> def get_parameter_product(num_parameters, lower_range, upper_range):
...     param_lists = (xrange(lower_range, upper_range) for _ in xrange(num_parameters))
...     return itertools.product(*param_lists)
>>> p = get_parameter_product(3, 0, 5)
>>> print(p)
<generator object <genexpr> at 0x101d650f0>
>>> print(list(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)]

Upvotes: 0

Related Questions