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:
- The keys in the dictionary should store the strings from the input list with their letters sorted alphabetically.
- 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:
''.join(sorted(word))