Darren Lefcoe
Darren Lefcoe

Reputation: 57

using the result of itertools.product multiple times

I use itertools.product in its usual form to generate a list of tuples.

I then iterate through the list with a standard for i in myList: loop to generate desired results.

I then wish to iterate a second time through the same loop using for i in myList:, but it does not execute the second loop without me regenerating the myList prior to loop entry.

Why is this ?

I have looked at the itertools help in the python docs which gives an explanation of what itertools.product() does, but it does not explain why the user would need to invoke before each loop entry.

https://docs.python.org/2/library/itertools.html#itertools.product

import itertools as it


def loopsThings(*lsts):


    # list of possibilities
    allPossible = it.product(a, b, c)

    for i in allPossible:
        print('first pass', i)

    # list of possibilities
    # second pass works if this is un-commented !!
    # allPossible = it.product(a, b, c) 

    for i in allPossible:
        print('second pass', i)

    return


a = [3, 5, 10]
b = [1, 9, 15]
c = [4, 8]

loopsThings(a, b, c)

i expect to get 2 lists, but only get 1 list (first pass)

in order to get the second list i need to run allPossible = it.product(a, b, c) before entry of the second loop.

Upvotes: 1

Views: 1518

Answers (2)

Darren Lefcoe
Darren Lefcoe

Reputation: 57

Using the previous comments:

This is the answer:

Some Python standard library functions like product return iterator objects that are only meant for a single-use iteration. Internally, they are like a pointer that points to an item in a linked list that moves up once every time a new item is fetched (through the next function).

The solution is to change from a (single use) object allPossible = it.product(a, b, c) to a list (using list comprehension works) allPossible = [x for x in it.product(a, b, c)]

Upvotes: 2

ParthS007
ParthS007

Reputation: 2689

itertools.product returns a <class 'itertools.product'>. You need to make a list of allPossible to get the expected result.

import itertools as it


def loopsThings(*lsts):


    # list of possibilities
    allPossible = list(it.product(a, b, c))

    for i in allPossible:
        print('first pass', i)

    # list of possibilities
    # second pass works if this is un-commented !!
    # allPossible = it.product(a, b, c) 

    for i in allPossible:
        print('second pass', i)

    return


a = [3, 5, 10]
b = [1, 9, 15]
c = [4, 8]

loopsThings(a, b, c)

Upvotes: 4

Related Questions