Python Forum
What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? (/thread-31223.html)



What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? - Pedroski55 - Nov-29-2020

I want to get all permutations of 2 vowels

from itertools import permutations

doubles = permutations (['a', 'e', 'i', 'o', 'u'],2) 
for i in doubles: 
    print(i) 
This prints all the permutations. I want to save them in a list.

I tried:

perm = ('a', 'e')
perm[0] gives me 'a'
perm[1] gives me 'e'
Now I find, if I run:

doubles = permutations (['a', 'e', 'i', 'o', 'u'],2)
for i in doubles:
    print(i)
I see my permutations

If I run

for i in doubles:
    print(i)
again, I get nothing. No error. Nothing.

Where has doubles gone? Seems it can only be read 1 time. Why is that? Read it and it is gone!

This gets me what I want:

doubles = permutations (['a', 'e', 'i', 'o', 'u'],2) 
perms = []
for i in doubles:
    print(i)
    perm = i[0] + i[1]
    perms.append(perm)



RE: What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? - buran - Nov-29-2020

once you iterate over it, the itertools.permutations object is exhausted and there is nothing to return on second attempt to use/iterate over it
from itertools import permutations
doubles = permutations(['a', 'e', 'i', 'o', 'u'], 2)
print(type(doubles))
doubles = list(permutations(['a', 'e', 'i', 'o', 'u'], 2))
print(type(doubles))
Output:
<class 'itertools.permutations'> <class 'list



RE: What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? - bowlofred - Nov-29-2020

Correct. The permutations() function returns an iterator. You can only consume an individual iterator once.

If you need the data again, you need to either recreate the iterator (call permutations() again), or save the information in another data structure. Memory requirements and access patterns may make one or the other a better choice.


RE: What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read? - DeaD_EyE - Nov-29-2020

Just hold a reference to the list/tuple and create new permutations and process them in the for-loop.
I made some other improvements.

# definition somewhere
doubles = ['a', 'e', 'i', 'o', 'u'] 

# if this never will change, choose a tuple
# this will prevent the ability to assign new objects to it
# doubles = ('a', 'e', 'i', 'o', 'u')
# then doubles[0] = "A" will raise an TypeError

perms = []
# creating always a new permutations object from doubles
for p in permutations(doubles, 2):
    # p is a tuple with permutations
    # we want to join them into a string
    p = "".join(p)
    # printing and appending
    print(p)
    perms.append(p)