Anagram check with Counter

EasyCollections Programs · 3 of 20

Write a Python program to check whether two words are anagrams using Counter.

Advertisement
Output
listen silent True
python typhon True
apple apply False

Approach

  1. Equal letter counts mean anagrams.

Solution

Python
from collections import Counter for a, b in [("listen", "silent"), ("python", "typhon"), ("apple", "apply")]:    print(a, b, Counter(a) == Counter(b))
Output
listen silent True
python typhon True
apple apply False

Learn the concept

New to this topic? Read the lesson, then come back.

More collections programs

← All 20 Collections Programs

Leave a Comment