Python - Group Similar Keys in Dictionary


In Python, similar keys in the dictionary can be grouped using various methods in Python as using defaultdict, using a dictionary of lists, using the itertools module, and the groupby function. During data analysis sometimes we may need to group similar keys together in a dictionary based on certain criteria. In this article, we will explore various methods to group similar keys in dictionaries.

Method 1: Using a defaultdict

Python's defaultdict class from the collections module provides a convenient way to group similar keys. It automatically initializes a default value when a new key is accessed.

Syntax

groups = defaultdict(list)
groups[item].append(item)

Here, defaultdict() function from the collections module creates a group containing empty list initially. The groups[item].append(item) method uses the key (item) to access the list linked with that key in the groups dictionary and appends the item to that list.

Example

In the below example, we create a defaultdict object called grouped_dict with a list as its default value. We iterate over each key−value pair and append the key to the corresponding list in grouped_dict. Finally, we convert the defaultdict to a regular dictionary using the dict() function.

from collections import defaultdict

def group_keys_defaultdict(keys):
    grouped_dict = defaultdict(list)
    for key in keys:
        grouped_dict[key[0]].append(key)
    return dict(grouped_dict)

keys = [('A', 1), ('B', 2), ('A', 3), ('C', 4), ('B', 5)]
grouped_dict = group_keys_defaultdict(keys)
print(grouped_dict)

Output

{'A': [('A', 1), ('A', 3)], 'B': [('B', 2), ('B', 5)], 'C': [('C', 4)]}

Method 2: Using a Dictionary of List

We can manually create an empty dictionary and iterate over the keys to group them by creating lists for each key.

Syntax

list_name.append(element)

Here, the append() function is a list method used to add an element to the end of the list_name. It modifies the original list by adding the specified element as a new item.

Example

In the below example, we initialize an empty dictionary called grouped_dict. For each key−value pair, we check if the key already exists in the dictionary. If not, we create an empty list as the value for that key. Then, we append the current key to the list. This way, we group similar keys together.

def group_keys_dict_of_lists(keys):
    grouped_dict = {}
    for key in keys:
        if key[0] not in grouped_dict:
            grouped_dict[key[0]] = []
        grouped_dict[key[0]].append(key)
    return grouped_dict

keys = [('A', 1), ('B', 2), ('A', 3), ('C', 4), ('B', 5)]
grouped_dict = group_keys_dict_of_lists(keys)
print(grouped_dict)

Output

{'A': [('A', 1), ('A', 3)], 'B': [('B', 2), ('B', 5)], 'C': [('C', 4)]}

Method 3: Using the GroupBy Function from the Itertools Module

We can use the groupby() function from the itertools module for grouping similar keys. It groups consecutive elements based on a key function.

Syntax

list_name.append(element)

Here, the append() function is a list method used to add an element to the end of the list_name. It modifies the original list by adding the specified element as a new item.

itertools.groupby(iterable, key=None)

Here, the iterable can be any group or collection of elements and key is an optional parameter that determines which grouping criteria will be applied. If the key is not passed it default value is None.

Example

In the below example, we first sort the keys based on the first element of each tuple using a lambda function as the key function. Then, we iterate over the sorted keys using groupby(). For each group, the key and the group itself are stored in separate variables. We convert the group to a list and store it in the grouped_dict with the corresponding key.

from itertools import groupby

def group_keys_itertools(keys):
    grouped_dict = {}
    keys.sort(key=lambda x: x[0])
    for key, group in groupby(keys, lambda x: x[0]):
        grouped_dict[key] = list(group)
    return grouped_dict

keys = [('A', 1), ('B', 2), ('A', 3), ('C', 4), ('B', 5)]
grouped_dict = group_keys_itertools(keys)
print(grouped_dict)

Output

{'A': [('A', 1), ('A', 3)], 'B': [('B', 2), ('B', 5)], 'C': [('C', 4)]}

Conclusion

In this article, we discussed how we can group similar keys in the dictionary using different methods in Python. We implemented three approaches: using a defaultdict, creating a dictionary of lists, and utilizing the groupby() function from the itertools module. Each method offers a unique way to achieve the desired result. By effectively using these methods, you can effectively group similar keys and organize your data in a more structured manner.

Updated on: 19-Jul-2023

856 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements