0

when calling set(c) second time it show empty set, it clear data butit working fine for first time and address of object is also same .

>>> import itertools
>>> a = ["aaa", "bbb", "ccc"]
>>> b = [True, False]
>>> c = itertools.product(a, b)
>>> c
<itertools.product object at 0x7f7bbca23050>
>>> set(c)
set([('bbb', True), ('ccc', True), ('ccc', False), ('aaa', True), ('bbb', False), ('aaa', False)])
>>> set(c)
set([])
>>> c
<itertools.product object at 0x7f7bbca23050>
Kallz
  • 3,244
  • 1
  • 20
  • 38

1 Answers1

0

you exhaust the generator c the first time you call set.

other example:

import itertools
a = ["aaa", "bbb", "ccc"]
b = [True, False]
c = itertools.product(a, b)

for item in c:
    print(item) 
for item in c:
    print(item)

the second loop will not print anything.

if you need to be able to iterate twice over the same iterator, you can use itertools.tee:

from itertools import tee

c1, c2 = tee(c)

now you have 2 independent iterators.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111