Challenge: Group Anagrams

This challenge is about finding the anagrams in a list.

Problem statement

Given a list of strings that contains anagrams, write a function to print pairs of those anagrams.


A list of strings. Remember that spaces count as characters. So, " abc" and “cab” are technically not anagrams, since " abc" has spaces which “cab” does not.


A list of lists where all pairs of anagrams grouped together

Sample input

input = [
    'tom marvolo riddle ',
    'clint eastwood ',
    'i am lord voldemort',
    'old west action',

Sample output

result = [['abc', 'cab'], ['clint eastwood ', 'old west action'], ['def', 'fed'], ['elvis', 'lives']]

Note: The order of the anagrams doesn’t matter!

Coding exercise

First, take a close look at this problem, design a step-by-step algorithm, and then, jump on to implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided in the next section.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy