anagrams in python


As a python programming exercise I set out to make anagrams where the user supplies letters to a program which after it consults a wordlist makes whatever words it can using all the letters. To keep it simpler I limited words to no fewer than three characters and disallowed repetition of words.

After the letters provided by the user have been read from the command-line it is time to read the wordlist and filter it to only the words that are possible with the available letters. Then the words are sorted in order by length to save time - once we have too few letters to make a certain word we have too few for any later word also and can avoid trying them.

There are various ways to generate the possible words so I tried four options. Three of these generate the whole list recursively then return it. The other generates a set of words each time you call it giving at least some result quickly and keeping memory required low. Source at github[1].

You are now equipped to spell ['onion', 'acceptor'] (by Grieg) - but not necessarily in that order. [2]


  1. github

  2. wikipedia
version 1
Written by Peter M Allan. 2017
linkedin back to articles