Group anagrams in a list of strings - ganeshahv/Contrail_SRE GitHub Wiki

Given a list of strings, group them by anagrams

#! /bin/python
from collections import defaultdict
dict_of_strings = defaultdict(list)
list_of_strings= ["cat", "ball", "act", "labl", "tac", "xyz", "xzy"]
for string in list_of_strings:
    new_string = ''.join(sorted(string))
    dict_of_strings[new_string].append(string)
print(' The set of Anagrams in the given list are %s' %(dict_of_strings.values()))