Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Group Anagrams from given list
In this tutorial, we are going to write a program that groups all anagrams in a list. First, let's see what are anagrams.
Any two strings that have the same characters in a different order are known as anagrams.
Before diving into the solution, let's see an example.
Example Input and Output
words = ['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac']
print("Input:", words)
Input: ['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac']
Expected output:
[['cat', 'tac'], ['dog', 'god'], ['fired', 'fried'], ['pat', 'tap']]
Understanding the Approach
We will breakdown the problem into two pieces. First, we will write a function that checks if two strings are anagrams or not. Follow the below steps to write code to check anagrams:
- Initialize the strings.
- Sort both the strings.
- If both sorted strings are equal then return True else False.
Checking Anagrams
# simple lambda function to check whether two strings are anagrams or not
are_anagrams = lambda x, y: str(sorted(x.lower())) == str(sorted(y.lower()))
# calling the function
print(are_anagrams('cat', 'tac'))
print(are_anagrams('cat', 'Tac'))
print(are_anagrams('cat', 'dog'))
True True False
Grouping Anagrams Using Dictionary
Now, we know how to check two strings whether they are anagrams or not. But, that's not enough to solve our problem. We need to group (store) all the anagrams from a list as sublists.
It's a best practice to use dictionaries to group the elements. We will have a single key for related anagrams. Let's see the steps to achieve what we want:
- Initialize the list of strings.
- Initialize an empty dictionary.
- Iterate over the list:
- Sort the string.
- Check whether it's present in the dictionary or not.
- If it's present in the dictionary, then append the string to its list.
- Else initialize the key with a list including the current string to store the anagrams.
- Return all the values of the dictionary in a list.
Complete Implementation
# initializing a list of strings
anagrams = ['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac']
# initializing an empty dict
grouped_anagrams = {}
# iterating over the list to group all anagrams
for string in anagrams:
# sorting the string
sorted_string = str(sorted(string))
# checking the string in dict
if sorted_string in grouped_anagrams:
# adding the string to the group anagrams
grouped_anagrams[sorted_string].append(string)
else:
# initializing a list with current string
grouped_anagrams[sorted_string] = [string]
# printing the values of the dict (anagram groups)
print(list(grouped_anagrams.values()))
[['cat', 'tac'], ['dog', 'god'], ['fired', 'fried'], ['pat', 'tap']]
Alternative Approach Using defaultdict
You can simplify the code using defaultdict from the collections module ?
from collections import defaultdict
words = ['cat', 'dog', 'fired', 'god', 'pat', 'tap', 'fried', 'tac']
# using defaultdict to avoid checking key existence
grouped = defaultdict(list)
for word in words:
sorted_key = ''.join(sorted(word))
grouped[sorted_key].append(word)
result = list(grouped.values())
print(result)
[['cat', 'tac'], ['dog', 'god'], ['fired', 'fried'], ['pat', 'tap']]
Comparison
| Method | Key Feature | Code Length |
|---|---|---|
| Basic Dictionary | Manual key checking | More verbose |
| defaultdict | Automatic list initialization | Cleaner code |
Conclusion
You can solve the anagram grouping problem using either basic dictionaries or defaultdict. The key insight is using sorted characters as dictionary keys to group anagrams together efficiently.
