What I\'m trying to do is if I have a list like:
[\"lime\", \"mile\", \"liem\", \"tag\", \"gat\", \"goat\", \"math\"]
I want to write a functio
an approach identifying the words by the frozenset
of the characters:
from collections import defaultdict
wordlist = ["lime", "mile", "liem", "tag", "gat", "goat", "math"]
worddict = defaultdict(list)
for word in wordlist:
worddict[frozenset(word)].append(word)
anagrams = [words for words in worddict.values() if len(words) > 1]
print(anagrams)
# [['lime', 'mile', 'liem'], ['tag', 'gat']]
the output is not yet quite what you wanted, but flattening that list is easy if you prefer that.
update after comments:
the solution above will not handle words with repeating characters well. but this will (this time the key of the dictionary is just the string consisting of the sorted letters):
for word in wordlist:
worddict[''.join(sorted(word))].append(word)