Print Anagrams together in Python using List and Dictionary
Last Updated :
25 Oct, 2025
Given a list of words, the task is to group all anagrams together in Python. Anagrams are words formed by rearranging the letters of another word, using all original letters exactly once. For example:
Input: ['bat', 'nat', 'tan', 'ate', 'eat', 'tea']
Output: [['bat'], ['nat', 'tan'], ['ate', 'eat', 'tea']]
Let's explore different methods to group anagrams together using list and dictionary in Python.
Using Counter() from collections
This method groups anagrams based on the frequency of each letter instead of sorting. It’s efficient for longer words because it avoids sorting every string.
Python
from collections import Counter, defaultdict
a = ['bat', 'nat', 'tan', 'ate', 'eat', 'tea']
res = defaultdict(list)
for word in a:
key = tuple(sorted(Counter(word).items()))
res[key].append(word)
print(list(res.values()))
Output[['bat'], ['nat', 'tan'], ['ate', 'eat', 'tea']]
Explanation:
- Counter(word) counts occurrences of each character (e.g., 'eat' -> {'e':1, 'a':1, 't':1}).
- sorted(Counter(word).items()) returns a sorted list of tuples.
- Converting it to a tuple makes it hashable as a dictionary key.
- All anagrams share the same frequency pattern and are grouped together.
Using defaultdict() from collections
This method sorts characters and uses the sorted result as a key to group words automatically.
Python
from collections import defaultdict
a = ['bat', 'nat', 'tan', 'ate', 'eat', 'tea']
res = defaultdict(list)
for word in a:
key = ''.join(sorted(word))
res[key].append(word)
print(list(res.values()))
Output[['bat'], ['nat', 'tan'], ['ate', 'eat', 'tea']]
Explanation:
- sorted(word) returns the sorted characters of the word.
- ''.join(sorted(word)) joins them to form a unique key.
- defaultdict(list) automatically creates a list if a key doesn’t exist, avoiding KeyError.
Using Dictionary
This method groups words by sorting their characters and using the sorted string as a key in a dictionary. All words with the same sorted characters fall under the same group.
Python
a = ['bat', 'nat', 'tan', 'ate', 'eat', 'tea']
res = {}
for word in a:
key = ''.join(sorted(word))
res[key] = res.get(key, []) + [word]
output = list(res.values())
print(output)
Output[['bat'], ['nat', 'tan'], ['ate', 'eat', 'tea']]
Explanation:
- sorted(word) sorts letters alphabetically (e.g., "eat" --> ['a', 'e', 't']).
- ''.join(sorted(word)) converts sorted characters back into a string ("aet").
- res.get(key, []) + [word] appends the word to the correct group.
- Finally, list(res.values()) returns all grouped lists of anagrams.
This method uses sorting and groupby() to group anagrams in a single line.
Python
from itertools import groupby
a = ['bat', 'nat', 'tan', 'ate', 'eat', 'tea']
a.sort(key=lambda x: ''.join(sorted(x)))
groups = [list(g) for _, g in groupby(a, key=lambda x: ''.join(sorted(x)))]
print(groups)
Output[['bat'], ['ate', 'eat', 'tea'], ['nat', 'tan']]
Explanation:
- a.sort(key=...) sorts the list by each word’s sorted character pattern.
- groupby() groups words that share the same sorted string.
- List comprehension [list(g) for _, g in groupby(...)] extracts each group.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice