0

I am trying to print the permutations of all the members in a list,but my script is printing the permutations of only last member of list i.e 'DMNCT'.

from itertools import permutations
element_all=['EESE', 'TTDT', 'SAIFE', 'DMNCT']
i=0
for i in range (len(element_all)):
    perms = [''.join(p) for p in permutations(element_all[i])]
print perms

It seems that my for loop is not working correctly.I am fairly new to python.Any help would be appreciated.

1
  • 3
    Each iteration of the for loop, you're overwriting the previous value of perms. You'll need to save them somewhere (append to a list, maybe?). Commented Nov 21, 2017 at 15:08

1 Answer 1

2

This is happening because you're replacing perms each loop. You should define the list outside the loop and then extend it inside the loop.

from itertools import permutations
element_all=['EESE', 'TTDT', 'SAIFE', 'DMNCT']
perms = []
for i in element_all:
    perms.extend([''.join(p) for p in permutations(i)])
print perms

Or define the list all at once in a comprehension

perms = [''.join(p) for i in element_all for p in permutations(i)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.