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
test_dict = { 'gfg1' : 1 , 'is1' : 2 , 'best1' : 3 ,
'gfg2' : 9 , 'is2' : 8 , 'best2' : 7 ,
'gfg3' : 10 , 'is3' : 5 , 'best3' : 6 }
print ( "The original dictionary is : " + str (test_dict))
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)
print ( "The grouped similar keys are : " + str (res))
|
Output
The 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
test_dict = { 'gfg1' : 1 , 'is1' : 2 , 'best1' : 3 ,
'gfg2' : 9 , 'is2' : 8 , 'best2' : 7 ,
'gfg3' : 10 , 'is3' : 5 , 'best3' : 6 }
print ( "The original dictionary is : " + str (test_dict))
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)
print ( "The grouped similar keys are : " + str (res))
|
Output
The 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
from collections import defaultdict
test_dict = { 'gfg1' : 1 , 'is1' : 2 , 'best1' : 3 ,
'gfg2' : 9 , 'is2' : 8 , 'best2' : 7 ,
'gfg3' : 10 , 'is3' : 5 , 'best3' : 6 }
print ( "The original dictionary is : " + str (test_dict))
res = defaultdict( dict )
for key, val in test_dict.items():
prefix, number = key[: - 1 ], key[ - 1 ]
res[prefix][number] = val
res = [ dict ( sorted (d.items())) for d in res.values()]
print ( "The grouped similar keys are : " + str (res))
|
Output
The 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
test_dict = { 'gfg1' : 1 , 'is1' : 2 , 'best1' : 3 ,
'gfg2' : 9 , 'is2' : 8 , 'best2' : 7 ,
'gfg3' : 10 , 'is3' : 5 , 'best3' : 6 }
print ( "The original dictionary is : " + str (test_dict))
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)
print ( "The grouped similar keys are : " + str (res))
|
Output
The 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.
Share your thoughts in the comments
Please Login to comment...