Python | Group Anagrams from given list

20 Jun, 2022
Anagrams are the words that are formed by similar elements but the orders in which these characters occur differ. Sometimes, we may encounter a problem in which we need to group the anagrams and hence solution to above problem always helps. Let’s discuss certain ways in which this can be done.Method #1 : Using defaultdict() + sorted() + values()The combination of above functions can be used to get the solution of above problem. In this, we first get the anagrams grouped using defaultdict and use sorted function to get each anagram root value to group anagrams.  
Python3
# Python3 code to demonstrate# Grouping Anagrams# using defaultdict() + sorted() + values()from collections import defaultdict # initializing listtest_list = ['lump', 'eat',  'me',  'tea', 'em', 'plum'] # printing original listprint("The original list : " + str(test_list)) # using defaultdict() + sorted() + values()# Grouping Anagramstemp = defaultdict(list)for ele in test_list:    temp[str(sorted(ele))].append(ele)res = list(temp.values()) # print resultprint("The grouped Anagrams : " + str(res))
The original list : ['lump', 'eat', 'me', 'tea', 'em', 'plum']
The grouped Anagrams : [['me', 'em'], ['lump', 'plum'], ['eat', 'tea']]
 
 Method #2 : Using list comprehension + sorted() + lambda + groupby()The combination of above function can also be used to perform this particular task. The groupby function performs the necessary grouping together. The lambda function helps to group alike anagrams.
Python3
# Python3 code to demonstrate# Grouping Anagrams# using list comprehension + sorted() + lambda + groupby()from itertools import groupby # initializing listtest_list = ['lump', 'eat',  'me',  'tea', 'em', 'plum'] # printing original listprint("The original list : " + str(test_list)) # using list comprehension + sorted() + lambda + groupby()# Grouping Anagramstemp = lambda test_list: sorted(test_list)res = [list(val) for key, val in groupby(sorted(test_list, key = temp), temp)] # print resultprint("The grouped Anagrams : " + str(res))
The original list : ['lump', 'eat', 'me', 'tea', 'em', 'plum']
The grouped Anagrams : [['me', 'em'], ['lump', 'plum'], ['eat', 'tea']]
 
Method3: Using Simple Python Programming
Below is the program to group anagram words together in a simple python programming way. Without using list comprehension or lambda or imported methods.
The method followed is simple – 
 1. Sort each word and use the sorted word as a dictionary key
 2. Keep adding new word into dictionary against sorted key
Python3
data = ['eat', 'ate', 'tea', 'ant', 'tan',        'bat', 'adobe', 'abode', 'listen', 'silent']  def createAnagramKey(string):    key = ''    for ch in sorted(string):        key += ch    return str(key)  def groupAnagramWords(data):    group = dict()    for ele in data:        if group.get(createAnagramKey(ele)) == None:            group[createAnagramKey(ele)] = [ele]        else:            group[createAnagramKey(ele)].append(ele)    return group  anagram_grouped = groupAnagramWords(data) # Anagram in dictionary formatprint('In dictionary format')print(anagram_grouped) anagram_grouped_list = list() for k, v in anagram_grouped.items():    anagram_grouped_list.append(v)print('In list format')print(anagram_grouped_list)
Output:
In dictionary format
{'aet': ['eat', 'ate', 'tea'], 'ant': ['ant', 'tan'], 'abt': ['bat'],
 'abdeo': ['adobe', 'abode'], 'eilnst': ['listen', 'silent']}
 
In list format
[['eat', 'ate', 'tea'], ['ant', 'tan'], ['bat'], ['adobe', 'abode'], ['listen', 'silent']]
nagendrakallihal
vinayedula
Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.