Skip to content
Related Articles

Related Articles

Improve Article

Print anagrams together in Python using List and Dictionary

  • Difficulty Level : Medium
  • Last Updated : 23 Nov, 2020

Given an array of words, print all anagrams together ?


Input : arr = ['cat', 'dog', 'tac', 'god', 'act']
Output : 'cat tac act dog god'

This problem has existing solution please refer Anagrams and Given a sequence of words, print all anagrams together links. We will solve this problem in python using List and Dictionary data structures. Approach is very simple :

  • Traverse list of strings.
  • Sort each string in ascending order and consider this sorted value as Key and original value as Value of corresponding key. Check if key is not present in dictionary that means it is occurring first time,
    so map a empty list to Key and append value in it, if key is already present then simple append the value.
  • Now each key will contain list of strings which are anagram together.

# Function to return all anagrams together 
def allAnagram(input): 
    # empty dictionary which holds subsets 
    # of all anagrams together 
    dict = {} 
    # traverse list of strings 
    for strVal in input
        # sorted(iterable) method accepts any 
        # iterable and rerturns list of items 
        # in ascending order 
        key = ''.join(sorted(strVal)) 
        # now check if key exist in dictionary 
        # or not. If yes then simply append the 
        # strVal into the list of it's corresponding 
        # key. If not then map empty list onto 
        # key and then start appending values 
        if key in dict.keys(): 
            dict[key] = [] 
    # traverse dictionary and concatenate values 
    # of keys together 
    output = "" 
    for key,value in dict.items(): 
        output = output + ' '.join(value) + ' '
    return output 
# Driver function 
if __name__ == "__main__"
    input=['cat', 'dog', 'tac', 'god', 'act'
    print (allAnagram(input)) 


'cat tac act dog god'

This article is contributed by Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :