# wordperms.py words = [ line.strip().lower() for line in open( 'ospd.txt' ) ] def is_valid_word( word, words ): if word in words: # return word in words return True else: return False import itertools while True: word = input( 'Enter word: ' ) if not word or not word.isalpha(): continue if word == 'abcd': break for perm in itertools.permutations( word ): print( perm, end = ' ' ) print( ''.join( perm ) ) perms = [ ''.join( x ) for x in itertools.permutations( word ) ] # deduplicate perms = sorted( list( set( perms ) ) ) # validate against the ospd.txt dictionary valid_words = list( filter( lambda x: x in words, perms ) ) invalid_words = list( itertools.filterfalse( lambda x: x in words, perms ) ) print( valid_words ) print( invalid_words )