0

What happens when you "empty" a temporary data structure

What happens when you "empty" a temporary data structure like a generator or the output of the itertools.product function? from itertools import product, permutations letters = ("A", "B") p=product(letters, range(2)) for n in p: print(n) for n in p: print(n) The output of the for loop, instead of a list, is each tuple line-by-line. The output of the final print is an error in in the product function with a memory address. with a list or set, popping out the values gives you [] or set()

30th Dec 2016, 11:48 PM
David Crowley
David Crowley - avatar
3 Réponses
+ 2
itertools.product gives a generator, so once you have exhausted it, it just throws StopIteration, cutting the for loop.
22nd Jan 2017, 1:55 AM
Amaras A
Amaras A - avatar
0
@david that code worked finely in my interpreter!!
22nd Jan 2017, 8:02 AM
Arun Manoharan
Arun Manoharan - avatar
0
Would you ever get thrown and error?
10th Feb 2017, 12:31 AM
Ryan Strittmatter