pythonpython-itertools

itertools permutation set loses its data after using len(list(my_set))


import itertools as itt
perm_set = itt.permutations('ABC')
iter1 = len(list(perm_set))
iter2 = len(list(perm_set))
print(iter1,iter2)

For some reason, after 3rd line `perm_set` loses its data, so `iter1!=iter2`. Why does it loose its data after taking the length of it?

But if i do the same thing with list, it works properly.

But if i do the same thing with list, it works properly.

l = [1,2,3,4,5]
iter1 = len(list(l))
iter2 = len(list(l))
print(iter1,iter2)

Solution

  • The functions in itertools return generators. Generators can only run once. After the first run, the generator is used up (no elements remain) and it will output no further elements. You can imagine this as a queue where you take one element out at a time and when the queue is empty, you won't be able to get any more elements from it anymore.

    In case you want to use the result of a generator more than once, you should store the list or re-create the generator.

    import itertools as itt
    perm_set = itt.permutations('ABC')
    perms = list(perm_set)
    iter1 = len(perms)
    iter2 = len(perms)
    print(iter1,iter2)
    

    or

    import itertools as itt
    perm_set = itt.permutations('ABC')
    iter1 = len(list(perm_set))
    perm_set = itt.permutations('ABC')
    iter2 = len(list(perm_set))
    print(iter1,iter2)
    

    Note, generators are lazy, they generate an element at the time when you ask for the next one. That's also why you cannot directly query their length. A list instead contains all elements at once, and consequently uses more memory than a generator usually would.