Python Forum
What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What happens to a <itertools.permutations object at 0x7fe3cc66af68> after it is read?
#1
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)
Reply
#2
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
Pedroski55 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
buran and Pedroski55 like this post
Reply
#4
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)
Pedroski55 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  itertools and amazing speed Pedroski55 8 1,985 Nov-11-2022, 01:20 PM
Last Post: Gribouillis
  Creating permutations of N size with no same elements next to one another (recursion) melmoman 1 1,504 Dec-18-2021, 09:39 PM
Last Post: Larz60+
  Making lists using itertools and eliminating duplicates. mike3891 2 2,190 Oct-26-2020, 05:39 PM
Last Post: bowlofred
  Python3 itertools.groupby printing the key august 1 2,046 Aug-17-2020, 05:46 AM
Last Post: bowlofred
  Generate Cartesian Products with Itertools Incrementally CoderMan 2 1,811 Jun-04-2020, 04:51 PM
Last Post: CoderMan
  [split] AttributeError: 'str' object has no attribute 'read' laxmipython 2 4,149 Feb-14-2020, 01:15 PM
Last Post: laxmipython
  itertools.zip_shortest() fo unequal iterators Skaperen 10 6,581 Dec-27-2019, 12:17 AM
Last Post: Skaperen
  Permutations mikke3141 2 19,142 Dec-23-2019, 06:09 PM
Last Post: mikke3141
  can itertools compact a list removing all of some value? Skaperen 6 3,102 Sep-02-2019, 03:19 AM
Last Post: Skaperen
  More Efficent Way of Generating Permutations/ w Rep ClassicalSoul 2 2,650 Aug-22-2019, 05:22 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020