How can I generate a list of all possible permutations of several letters?

codeman99 picture codeman99 · Jan 29, 2014 · Viewed 11.2k times · Source

So I am making a word generator that takes several inputted letters, puts them in all possible positions, and matches them with a document to find words. If I am approaching this wrong please tell me! If not how can I do this? Thanks

Answer

Colin Bernet picture Colin Bernet · Jan 29, 2014

to generate all permutations of a given list of letters, use the itertools module.

import itertools 
for word in itertools.permutations( list_of_letters ):
   print ''.join(word)