Python - Group Similar keys in dictionary
Last Updated :
15 May, 2023
Sometimes while working with dictionary data, we can have problems in which we need to perform grouping based on the substring of keys and reform the data grouped on similar keys. This can have application in data preprocessing. Let us discuss certain ways in which this task can be performed.
Method #1: Using loop
This is brute way in which we perform this task. In this, we check for elements using conditional statement and insert the keys according to substring presence.
Python3
# Python3 code to demonstrate working of
# Group Similar keys in dictionary
# Using loop
# initializing Dictionary
test_dict = {'gfg1' : 1, 'is1' : 2, 'best1' : 3,
'gfg2' : 9, 'is2' : 8, 'best2' : 7,
'gfg3' : 10, 'is3' : 5, 'best3' : 6}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Group Similar keys in dictionary
# Using loop
res = []
res1, res2, res3 = {}, {}, {}
for key, value in test_dict.items():
if 'gfg' in key:
res1[key] = value
elif 'is' in key:
res2[key] = value
elif 'best' in key:
res3[key] = value
res.append(res1)
res.append(res2)
res.append(res3)
# printing result
print("The grouped similar keys are : " + str(res))
OutputThe original dictionary is : {'gfg1': 1, 'is1': 2, 'best1': 3, 'gfg2': 9, 'is2': 8, 'best2': 7, 'gfg3': 10, 'is3': 5, 'best3': 6}
The grouped similar keys are : [{'gfg1': 1, 'gfg2': 9, 'gfg3': 10}, {'is1': 2, 'is2': 8, 'is3': 5}, {'best1': 3, 'best2': 7, 'best3': 6}]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using dictionary comprehension
This is yet another way in which this task can be performed. In this, we group the substring to keys using dictionary comprehension.
Python3
# Python3 code to demonstrate working of
# Group Similar keys in dictionary
# using dictionary comprehension
# Initializing Dictionary
test_dict = {'gfg1' : 1, 'is1' : 2, 'best1' : 3,
'gfg2' : 9, 'is2' : 8, 'best2' : 7,
'gfg3' : 10, 'is3' : 5, 'best3' : 6}
# Printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Group Similar keys in dictionary
# using dictionary
res = []
res1 = {key : val for key, val in test_dict.items() if 'gfg' in key}
res2 = {key : val for key, val in test_dict.items() if 'is' in key}
res3 = {key : val for key, val in test_dict.items() if 'best' in key}
res.append(res1)
res.append(res2)
res.append(res3)
# Printing result
print("The grouped similar keys are : " + str(res))
OutputThe original dictionary is : {'gfg1': 1, 'is1': 2, 'best1': 3, 'gfg2': 9, 'is2': 8, 'best2': 7, 'gfg3': 10, 'is3': 5, 'best3': 6}
The grouped similar keys are : [{'gfg1': 1, 'gfg2': 9, 'gfg3': 10}, {'is1': 2, 'is2': 8, 'is3': 5}, {'best1': 3, 'best2': 7, 'best3': 6}]
Time Complexity: O(n*n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3: Using the defaultdict from the collections module:
Approach:
- Import the defaultdict class from the collections module.
- Initialize the test_dict with the given keys and values.
- Print the original dictionary.
- Initialize an empty defaultdict with a dict factory.
- Loop through the keys and values in the test_dict.
- Extract the prefix and number of each key using slicing.
- Add the value to the dictionary corresponding to the prefix and number.
- Convert the defaultdict to a list of dictionaries.
- Sort each dictionary in the list based on the keys.
- Print the result.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of
# Group Similar keys in dictionary
# using defaultdict
from collections import defaultdict
# Initializing Dictionary
test_dict = {'gfg1': 1, 'is1': 2, 'best1': 3,
'gfg2': 9, 'is2': 8, 'best2': 7,
'gfg3': 10, 'is3': 5, 'best3': 6}
# Printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Group Similar keys in dictionary
# using defaultdict
res = defaultdict(dict)
for key, val in test_dict.items():
prefix, number = key[:-1], key[-1]
res[prefix][number] = val
# Convert the defaultdict to list of dictionaries
res = [dict(sorted(d.items())) for d in res.values()]
# Printing result
print("The grouped similar keys are : " + str(res))
OutputThe original dictionary is : {'gfg1': 1, 'is1': 2, 'best1': 3, 'gfg2': 9, 'is2': 8, 'best2': 7, 'gfg3': 10, 'is3': 5, 'best3': 6}
The grouped similar keys are : [{'1': 1, '2': 9, '3': 10}, {'1': 2, '2': 8, '3': 5}, {'1': 3, '2': 7, '3': 6}]
Time complexity: O(n log n) due to sorting the dictionaries.
Auxiliary space: O(n) due to the use of the defaultdict.
Method #4: Using using itertools.groupby():
To group similar keys in a dictionary, we can use the groupby() function from itertools module. First, we sort the dictionary keys using sorted(). Then, we group them based on their similarity using a lambda function. This function returns the key without the last character. We create a temporary dictionary for each group using a dictionary comprehension and append it to the final list of grouped dictionaries.
Python3
from itertools import groupby
# Initializing Dictionary
test_dict = {'gfg1': 1, 'is1': 2, 'best1': 3,
'gfg2': 9, 'is2': 8, 'best2': 7,
'gfg3': 10, 'is3': 5, 'best3': 6}
# Printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Group Similar keys in dictionary using itertools.groupby()
res = []
for key, group in groupby(sorted(test_dict), key=lambda x: x[:-1]):
temp_dict = {k: test_dict[k] for k in group}
res.append(temp_dict)
# Printing result
print("The grouped similar keys are : " + str(res))
OutputThe original dictionary is : {'gfg1': 1, 'is1': 2, 'best1': 3, 'gfg2': 9, 'is2': 8, 'best2': 7, 'gfg3': 10, 'is3': 5, 'best3': 6}
The grouped similar keys are : [{'best1': 3, 'best2': 7, 'best3': 6}, {'gfg1': 1, 'gfg2': 9, 'gfg3': 10}, {'is1': 2, 'is2': 8, 'is3': 5}]
Time Complexity: O(nlogn), where n is the number of keys in the dictionary.
Auxiliary Space: O(n), where n is the number of keys in the dictionary.
Similar Reads
Python | Grouping dictionary keys by value
While performing computations over dictionary, we can come across a problem in which we might have to perform the task of grouping keys according to value, i.e create a list of keys, it is value of. This can other in cases of organising data in case of machine learning. Let's discuss certain way in
4 min read
Python - Group similar value list to dictionary
Sometimes, while working with Python list, we can have a problem in which we need to group similar value lists indices to values into a dictionary. This can have a good application in domains in which we need grouped dictionary as output for pair of lists. Lets discuss certain ways in which this tas
6 min read
Get Total Keys in Dictionary - Python
We are given a dictionary and our task is to count the total number of keys in it. For example, consider the dictionary: data = {"a": 1, "b": 2, "c": 3, "d": 4} then the output will be 4 as the total number of keys in this dictionary is 4.Using len() with dictThe simplest way to count the total numb
2 min read
Python - Group Similar items to Dictionary Values List
We are given a list of items and our task is to group similar elements together as dictionary values. The keys will be the unique items and their values will be lists containing all occurrences of that item. For example, given ['apple', 'banana', 'apple', 'orange', 'banana'], the output should be: {
2 min read
Python - Summation Grouping in Dictionary List
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to perform the grouping of dictionaries according to specific key, and perform summation of certain keys while grouping similar key's value. This is s peculiar problem but can have applications in domains such
5 min read
Python program to group keys with similar values in a dictionary
Given a dictionary with values as a list. Group all the keys together with similar values. Input : test_dict = {"Gfg": [5, 6], "is": [8, 6, 9], "best": [10, 9], "for": [5, 2], "geeks": [19]} Output : [['Gfg', 'is', 'for'], ['is', 'Gfg', 'best'], ['best', 'is'], ['for', 'Gfg']] Explanation : Gfg has
2 min read
Python - Group Hierarchy Splits of keys in Dictionary
Given a dictionary with keys joined by a split character, the task is to write a Python program to turn the dictionary into nested and grouped dictionaries. Examples Input: test_dict = {"1-3" : 2, "1-8" : 10}, splt_chr = "-"Output: {'1': {'3': 2, '8': 10}}Explanation : 1 is linked to 3 with value 2
5 min read
Python | Grouping list values into dictionary
Sometimes, while working with data, we can be encountered a situation in which we have a list of lists and we need to group its 2nd index with the common initial element in lists. Let's discuss ways in which this problem can be solved. Method 1: Using defaultdict() + loop + dict() The defaultdict ca
7 min read
How to Print Dictionary Keys in Python
We are given a dictionary and our task is to print its keys, this can be helpful when we want to access or display only the key part of each key-value pair. For example, if we have a dictionary like this: {'gfg': 1, 'is': 2, 'best': 3} then the output will be ['gfg', 'is', 'best']. Below, are the me
2 min read
Key Index in Dictionary - Python
We are given a dictionary and a specific key, our task is to find the index of this key when the dictionaryâs keys are considered in order. For example, in {'a': 10, 'b': 20, 'c': 30}, the index of 'b' is 1.Using dictionary comprehension and get()This method builds a dictionary using dictionary comp
2 min read