Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Counting with dictionaries
Grouping with dictionaries
12. Grouping with dictionaries – anagrams
Linking with dictionaries


It's time for an additional exercise. We will present the kind of question you may get in a coding interview, so stay focused!


Anagrams are words which contain the same letters in a different order (for example: dear, read, and dare are anagrams). Write a function find_anagrams(list) which accepts a list of words (strings) and returns a dictionary with the following properties:

  1. The keys in the dictionary should store the strings from the input list with their letters sorted alphabetically.
  2. The value of each key should contain a list of words from the input list which are anagrams for the given key.

For example, for input:

['bacd', 'bcad', 'adf']

the output should be:

{'abcd': ['bacd', 'bcad'], 'adf': ['adf']}

Explanation: Both 'bacd' and 'bcad' are anagrams. When sorted alphabetically, they yield 'abcd'. That's why we put them under the 'abcd' key.

To quickly sort the letters in a given string alphabetically and get a new ordered string, use the following code: