Editor

Group the Anagrams

Intermediate · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Write group_anagrams(words) that groups words that are anagrams of each other. Return a list of groups; groups appear in the order their first word appears, and words keep their original order inside each group.

group_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"])
->  [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
def group_anagrams(words):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
group_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"])[['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]
group_anagrams([])[]

More practice problems

All Python exercises · 50 practice problems