Python Program to Find Anagram

An anagram is a word formed by rearranging the letters of another word, such as cinema, formed from iceman. In this article, we will discuss a Python program to find anagrams.

Python Program: Find Anagram

To find anagrams, we need to compare each word in a list with every other word in the list. To efficiently achieve this, we can create a dictionary where the keys represent the sorted letters of each word and the values represent a list of words that contain those letters.

def find_anagrams(words):
anagrams = {}
for word in words:
sorted_word = ''.join(sorted(word))
if sorted_word in anagrams:
anagrams[sorted_word].append(word)
else:
anagrams[sorted_word] = [word]
return [anagrams[key] for key in anagrams if len(anagrams[key]) > 1]

In this program, we first create an empty dictionary called `anagrams`. We then iterate through each word in the input list of words. For each word, we sort its letters and combine them to create a new string, `sorted_word`. We then use this sorted word as a key in the `anagrams` dictionary. If the key already exists, we append the current word to the list of words in the corresponding value. If the key doesn’t exist, we create a new list with the current word as its only element.

Finally, we return a list of lists, where each inner list contains all the words that are anagrams of each other.

Examples

Let’s see some examples of how the program works.

>>> find_anagrams(['cinema', 'iceman', 'listen', 'silent', 'debitcard', 'badcredit'])
[['cinema', 'iceman'], ['listen', 'silent'], ['debitcard', 'badcredit']]

In this example, the program correctly identifies three sets of anagrams: cinema and iceman, listen and silent, and debitcard and badcredit.

>>> find_anagrams(['cat', 'dog', 'act', 'god'])
[['cat', 'act'], ['dog', 'god']]

In this example, the program identifies two sets of anagrams: cat and act, and dog and god.

Frequently Asked Questions (FAQs)

1. What is an anagram?

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.

2. How does the Python program to find anagrams work?

The Python program to find anagrams works by creating a dictionary where each key represents the sorted letters of a word and the corresponding value is a list of words that contain that sorted set of letters. The program then iterates through each word in the input list of words and adds it to the value list of the corresponding sorted key. Finally, the program returns a list of lists, where each inner list contains all the words that are anagrams of each other.

3. Can the program find anagrams in a sentence?

No, the program as written only looks for anagrams in a list of words. However, it can be modified to handle sentences by splitting the sentence into individual words and passing the resulting list of words to the function.

4. Is there an alternative method to find anagrams in Python?

Yes, there are alternative methods to find anagrams in Python, such as using collections.Counter to count the frequency of letters in a word. However, the method demonstrated in this article is a simple and efficient approach.

Facebook
Twitter
LinkedIn
Pinterest

Table of Contents

Related posts