Detecting Anagrams Using Python

Michael Galarnyk
Analytics Vidhya
Published in
3 min readJul 6, 2019

--

GIF showing that the words percussion and supersonic are anagrams of each other. Original Video Source: https://player.vimeo.com/video/233358575 from https://vimeo.com/c4djed

Introduction

In the GIF above, the words supersonic and percussion have the exact same letters used. The words are anagrams of each other. For example, Car and Arc are anagrams of each other as well as Leaf and Flea.

In this blog, we will build a simple code using python that can identify anagrams from the given input.

Problem Statement

Task: Write a program that takes in a word list and outputs a list of all the words that are anagrams of another word in the list.

Before starting, it is important to note what an anagram is. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

While there are many different ways to solve this problem, this blog gives two different approaches to solve this problem.

For both of the approaches below, we first need to define a word list.

word_list = ["percussion", "supersonic", "car", "tree", "boy", "girl", "arc"]

Approach 1: For Loops and Sorted

# initialize a list
anagram_list = []
for word_1 in word_list:
for word_2 in word_list:
if word_1 != word_2 and (sorted(word_1)==sorted(word_2)):
anagram_list.append(word_1)
print(anagram_list)

If you look at the inner for loop enclosed in the red rectangle above, the code word_1 != word_2 checks that the words are not the same. The inbuilt function sorted converts each word from a string to a list of characters which you can see in the example below. Since sorted('percussion') == sorted('supersonic') is True, they are anagrams of each other.

Approach 2: Dictionaries

def freq(word):
freq_dict = {}
for char in word:
freq_dict[char] = freq_dict.get(char, 0) + 1
return freq_dict
# initialize a list
anagram_list = []
for word_1 in word_list:
for word_2 in word_list:
if word_1 != word_2 and (freq(word_1) == freq(word_2)):
anagram_list.append(word_1)
print(anagram_list)
For the freq function, If you have difficulty understanding the dictionary get method, I encourage you to see one of the following tutorials Python Dictionary and Dictionary Methods or Python Word Count

If you look at the inner for loop enclosed in the red rectangle above, the code freq(word_1) == freq(word_2) checks that the words are not the same. The function freq converts each word to a dictionary of char frequency. For example, since freq('percussion') == freq('supersonic') is True, they are anagrams of each other.

As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. While the ordering of the output looks different, both of the outputs have the same key-value pairs.

For the dictionaries above, notice that the ordering of the output is different. This is because as of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. In the example below, both of the outputs have the same key-value pairs which means that freq('percussion') == freq('supersonic').

Concluding Remarks

The two approaches are definitely not the only ways to detect anagrams, but hopefully this blog helped you. Feel free to share your ideas or approaches in the comment section.

As always, the code is also available on my GitHub. If you any questions or thoughts on the tutorial, feel free to reach out in the comments below or through Twitter. If you want to learn how to utilize the Pandas, Matplotlib, or Seaborn libraries, please consider taking my Python for Data Visualization LinkedIn Learning course.

--

--