Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Grouping dictionary keys by value

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




# Python3 code to demonstrate working of
# Grouping dictionary keys by value
# Using sorted() + items() + defaultdict()
from collections import defaultdict
 
# Initialize dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 1, 'for' : 3, 'CS' : 2}
 
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
 
# Using sorted() + items() + defaultdict()
# Grouping dictionary keys by value
res = defaultdict(list)
for key, val in sorted(test_dict.items()):
    res[val].append(key)
     
# printing result
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




# Python3 code to demonstrate working of
# Grouping dictionary keys by value
# Using itertools.groupby()
from itertools import groupby
  
# Initialize dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 1, 'for' : 3, 'CS' : 2}
  
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
  
# Using itertools.groupby()
# Grouping dictionary keys by value
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])}
  
# printing result
print("Grouped dictionary is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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:

  1. Initialize an empty dictionary to store the grouped keys.
  2. Iterate through the items of the original dictionary.
  3. For each item, check if the value is already a key in the grouped dictionary.
  4. If it is not, add the value as a key and initialize its value as a list containing the key from the original dictionary.
  5. If it is already a key, append the key from the original dictionary to the list of values associated with that key.
  6. Return the grouped dictionary.

Python3




# Python3 code to demonstrate working of
# Grouping dictionary keys by value
# Using a loop
  
# Initialize dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 1, 'for' : 3, 'CS' : 2}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# Using a loop
# Grouping dictionary keys by value
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)
  
# printing result
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.

RELATED ARTICLES

Most Popular

Recent Comments