Anagram checking in Python program using collections.Counter()



An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. For example, thing and night are anagrams of each other.

In this article, we are going to learn how to check an anagram in a Python program using collections.Counter() method. This method counts the number of occurrences of each element in a collection (like characters in a string), helping us to compare two words for identical character counts. For example,

Scenario
Input: str1= "listen", str2= "silent"
Output: True

Two strings are said to be anagrams when using the collections.Counter(), only if their counter representations are equal.

Counter("listen") = {'l':1, 'i':1, 's':1, 't':1, 'e':1, 'n':1}
Counter("silent") = {'s':1, 'i':1, 'l':1, 'e':1, 'n':1, 't':1}

Here, both the strings have identical character counts, hence they are anagrams. Two strings are said to be anagrams of each other if they have the same characters, even in a different order.

The collections.Counter() Method

The Counter is a subclass of the dict class in the collections module. It is used for counting items available or existing in iterables. For counting characters in a string, we need to use any non-negative integer values (including zero).

Syntax

Following is the syntax of collections.Counter() method -

from collections import Counter
Counter(iterable)

In the examples given below, we will consider two strings and convert each to a counter object, then compare them (using "=="). If the counters are equal, the strings are anagrams of each other.

Example 1

Let's look at the following example, where we are going to consider the two strings ("listen", "silent") and check whether they are anagrams or not.

from collections import Counter
def demo(x1, x2):
   return Counter(x1) == Counter(x2)
print(demo("listen", "silent"))

The output of the above program is as follows -

True

Example 2

Consider another scenario where we are going to check whether two strings, given as phrases, are anagrams of each other.

from collections import Counter
def demo(str1, str2):
   x1 = str1.replace(" ", "").lower()
   x2 = str2.replace(" ", "").lower()
   return Counter(x1) == Counter(x2)
print(demo("School master", "The classroom"))

Following is the output of the above program -

True

Example 3

In the following example, we are going to check whether the two strings are anagrams or not by considering two strings of different lengths.

from collections import Counter
def demo(str1, str2):
   x1 = str1.replace(" ", "").lower()
   x2 = str2.replace(" ", "").lower()
   return Counter(x1) == Counter(x2)
print(demo("Hi", "hiiii"))

If we run the above program, it will generate the following output -

False

Conclusion

Using collections.Counter() method is an easy way to check if the two strings are anagrams or not. It avoids the need to manually count characters.

Updated on: 2025-07-14T15:08:00+05:30

244 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements