Print anagrams together in Python using List and Dictionary Last Updated : 11 Feb, 2025 Comments Improve Suggest changes Like Article Like Report An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using all the original letters exactly once. The task of grouping anagrams together in Python can be efficiently solved using lists and dictionaries. The key idea is to process each word by sorting its characters, which helps identify anagrams as they will share the same sorted representation. By using a dictionary, we can store these sorted words as keys and group the original words as values, ensuring that all anagrams appear together. For example, given a list a = ['bat', 'nat', 'tan', 'ate', 'eat', 'tea'], the expected output would be:[['bat'], ['nat', 'tan'], ['ate', 'eat', 'tea']]. Python dict = {} # initialize empty dictionary a = ['bat', 'nat', 'tan', 'ate', 'eat', 'tea'] # Iterate through each word for word in a: sort_word = ''.join(sorted(word)) if sort_word in dict: dict[sort_word].append(word) else: dict[sort_word] = [word] res = list(dict.values()) print(res) Output[['bat'], ['nat', 'tan'], ['ate', 'eat', 'tea']] Explanation:sorted(word) arranges the letters of the word in alphabetical order and ''.join(sorted(word)) converts the sorted list of characters back into a string.If sort_word is already a key in dict, append the current word to its corresponding Otherwise, create a new entry in dict with sort_word as the key and the word as the first element of a new list.list(dict.values()) extracts those lists and stores them in res. Comment More infoAdvertise with us Next Article Print anagrams together in Python using List and Dictionary S Shashank Mishra Follow Improve Article Tags : Strings Python DSA anagram python-list python-dict Python list-programs Python dictionary-programs +4 More Practice Tags : anagrampythonpython-dictpython-listStrings +1 More Similar Reads Ways to create a dictionary of Lists - Python A dictionary of lists is a type of dictionary where each value is a list. These dictionaries are commonly used when we need to associate multiple values with a single key.Initialize a Dictionary of ListsThis method involves manually defining a dictionary where each key is explicitly assigned a list 3 min read Convert Two Lists into a Dictionary - Python We are given two lists, we need to convert both of the list into dictionary. For example we are given two lists a = ["name", "age", "city"], b = ["Geeks", 30,"Delhi"], we need to convert these two list into a form of dictionary so that the output should be like {'name': 'Geeks', 'age': 30, 'city': ' 3 min read Combine similar characters in Python using Dictionary Get() Method Let us see how to combine similar characters in a list. Example : Input : ['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's'] Output : ['gg', 'eeee', 'kk', 'ss', 'f', 'o', 'r'] We will be using the get() method of the dictionary class. dictionary.get() The get() method returns the valu 3 min read Python | Convert list of tuple into dictionary Given a list containing all the element and second list of tuple depicting the relation between indices, the task is to output a dictionary showing the relation of every element from the first list to every other element in the list. These type of problems are often encountered in Coding competition 8 min read Anagram checking in Python using collections.Counter() Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, âabcdâ and âdabcâ are anagram of each other. Examples: Input : str1 = âabcdâ, str2 2 min read Like