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 which this task can be performed.
Method 1: Using sorted() + items() + defaultdict()
This task can be performed by combining the tasks which can be done by above functions. The defaultdict() is used to create a dictionary initialized with lists, items() gets the key-value pair and grouping is helped by sorted().
Python3
from collections import defaultdict
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 1 , 'for' : 3 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
res = defaultdict( list )
for key, val in sorted (test_dict.items()):
res[val].append(key)
print ( "Grouped dictionary is : " + str ( dict (res)))
|
Output:
The original dictionary : {'gfg': 1, 'is': 2, 'best': 1, 'for': 3, 'CS': 2}
Grouped dictionary is : {2: ['CS', 'is'], 1: ['best', 'gfg'], 3: ['for']}
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 2:
Additionally, This task can also be performed without using any module.
So the logic here is:
We can check if the keys are present or not
1. No, then we can create key res[v] = [i]
2. Yes, we can append value on the key res[v] + [i]
Python3
d_input = { 'Input.txt' : 'Randy' , 'Code.py' : 'Stan' , 'Output.txt' : 'Randy' }
res = {}
for i, v in d_input.items():
res[v] = [i] if v not in res.keys() else res[v] + [i]
print (res)
|
Output:
{'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}
Method 3: Using itertools.groupby()
Itertools.groupby() is a tool used to group elements based on a key. The key is a function that is applied to each element in order to determine its group. In this case, we can use the same dictionary.items() method to provide the key to the itertools.groupby()
Python3
from itertools import groupby
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 1 , 'for' : 3 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
res = {i: [j[ 0 ] for j in j] for i, j in groupby( sorted (test_dict.items(), key = lambda x : x[ 1 ]), lambda x : x[ 1 ])}
print ( "Grouped dictionary is : " + str (res))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 1, 'for': 3, 'CS': 2}
Grouped dictionary is : {1: ['gfg', 'best'], 2: ['is', 'CS'], 3: ['for']}
Time Complexity: O(n log n)
Auxiliary Space: O(n)
Method 4 : Using a loop
Step-by-step approach:
- Initialize an empty dictionary to store the grouped keys.
- Iterate through the items of the original dictionary.
- For each item, check if the value is already a key in the grouped dictionary.
- If it is not, add the value as a key and initialize its value as a list containing the key from the original dictionary.
- If it is already a key, append the key from the original dictionary to the list of values associated with that key.
- Return the grouped dictionary.
Python3
test_dict = { 'gfg' : 1 , 'is' : 2 , 'best' : 1 , 'for' : 3 , 'CS' : 2 }
print ( "The original dictionary : " + str (test_dict))
grouped_dict = {}
for key, value in test_dict.items():
if value not in grouped_dict:
grouped_dict[value] = [key]
else :
grouped_dict[value].append(key)
print ( "Grouped dictionary is : " + str (grouped_dict))
|
Output
The original dictionary : {'gfg': 1, 'is': 2, 'best': 1, 'for': 3, 'CS': 2}
Grouped dictionary is : {1: ['gfg', 'best'], 2: ['is', 'CS'], 3: ['for']}
The time complexity of this method is O(n), where n is the number of items in the dictionary.
The auxiliary space complexity is also O(n), because the grouped dictionary may contain all the keys and values from the original dictionary.
Last Updated :
10 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...