Python Group Anagrams from given list

PythonServer Side ProgrammingProgramming

In this tutorial, we are going to write a program that groups all anagrams in a list. First, let's see what are anagrams.

Any two strings that have the same character in a different order are known as anagrams.

Before diving into the solution, let's see an example.


['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac']


[['cat', 'tac'], ['dog', 'god'], ['fried', 'fired'], ['pat', 'tap']]

We will breakdown the problem into two pieces. First, we will write a function that checks two strings are anagrams or not. Follow the below steps to write code to check anagrams.

  • Initialize the strings.
  • Sort both the strings.
  • If both sorted strings are equal then return True else False.


 Live Demo

# simple lambda function to check whether two strings are anagrams or not
are_anagrams = lambda x, y: str(sorted(x.lower())) == str(sorted(y.lower()))
# calling the function
print(are_anagrams('cat', 'tac'))
print(are_anagrams('cat', 'Tac'))
print(are_anagrams('cat', 'dog'))


If you run the above code, then you will get the following result.


Now, we know how to check two strings whether they are anagrams or not. But, that's not enough to solve our problem. We need to group (store) all the anagrams from a list as sublists.

How can we solve the problem?

It's a best practice to use the dictionaries to group the elements. We will have a single key for related anagrams. It's a bit confusing if you are new to Python. Let's see the steps to achieve what we want.

  • Initialize the list of strings.
  • Initialize an empty dictionary.
  • Iterate over the list.
    • Sort the string.
    • Check whether it's present in the dictionary or not.

      • If it present in the dictionary, then append the string to its list.
    • Else initialize the key with a list including the current string to store the anagrams.
  • Print all the values of the dictionary in a list.


 Live Demo

# initialzing a list of strings
anagrams = ['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac']
# initializing an empty dict
grouped_anagrams = {}
# iterating over the list to group all anagrams
for string in anagrams:
   # sorting the string
   sorted_string = str(sorted(string))
   # checking the string in dict
   if sorted_string in grouped_anagrams:
      # adding the string to the group anagrams
         # initializing a list with current string
         grouped_anagrams[sorted_string] = [string]
# printing the values of the dict (anagram groups)


If you run the above code, then you will get the following result.

[['dog', 'god'], ['pat', 'tap'], ['cat', 'tac'], ['fired', 'fried']]


You can solve the problem using different approaches as well. There a data structure called defaultdict that helps you avoid checking for the key in the dictionary. You can explore it and change the code accordingly.

If you have any doubts in the tutorial, mention them in the comment section.

Published on 06-Jul-2020 09:37:07