Python - Group Hierarchy Splits of keys in Dictionary
Last Updated :
30 Apr, 2023
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 and 8 with value 10, hence those elements are nested and values assigned.
Input: test_dict = {"1-3" : 2, "8-7" : 0, "1-8" : 10, "8-6" : 15}, splt_chr = "-"
Output: {'1': {'3': 2, '8': 10}, '8': {'7': 0, '6': 15}}
Explanation : 1 is linked to 3 with value 2 and 8 with value 10, similarly 8 is linked to 7 with value 0 and 6 with value 15, hence those elements are nested and values assigned.
Method #1 : Using loop + split()
In this, we perform the task of splitting the keys for flattening using split(). And then memorization of keys is done using dictionary which adds to its nested dictionaries other similar nested keys found.
Python3
# Python3 code to demonstrate working of
# Group Hierarchy Splits of keys in Dictionary
# Using loop + split()
# initializing dictionary
test_dict = {"1-3" : 2, "8-7" : 0, "1-8" : 10, "8-6" : 15}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing split char
splt_chr = "-"
res = dict()
for key, val in test_dict.items():
ini_key, low_key = key.split(splt_chr)
# check if key already present
if ini_key not in res:
res[ini_key] = dict()
# add nested value if present key
res[ini_key][low_key] = val
# printing result
print("The splitted dictionary : " + str(res))
OutputThe original dictionary is : {'1-3': 2, '8-7': 0, '1-8': 10, '8-6': 15}
The splitted dictionary : {'1': {'3': 2, '8': 10}, '8': {'7': 0, '6': 15}}
Similar to the above method, the Only difference being defaultdict() is used for the task of memorizing the grouping keys.
Python3
# Python3 code to demonstrate working of
# Group Hierarchy Splits of keys in Dictionary
# Using defaultdict()
from collections import defaultdict
# initializing dictionary
test_dict = {"1-3" : 2, "8-7" : 0, "1-8" : 10, "8-6" : 15}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing split char
splt_chr = "-"
res = defaultdict(dict)
for key, val in test_dict.items():
ini_key, low_key = key.split(splt_chr)
# defaultdict eliminates check step
res[ini_key][low_key] = val
# printing result
print("The splitted dictionary : " + str(dict(res)))
OutputThe original dictionary is : {'1-3': 2, '8-7': 0, '1-8': 10, '8-6': 15}
The splitted dictionary : {'1': {'3': 2, '8': 10}, '8': {'7': 0, '6': 15}}
Method #3: Using recursive function
Approach
using a recursive function to split the keys and create nested dictionaries based on the split parts. The function takes in a list of keys and a value and creates a nested dictionary based on the split parts of the keys.
Algorithm
1. Initialize an empty dictionary called result.
2. Define a recursive function called add_to_dict that takes in a list of keys and a value.
3. If the list of keys is empty, return the value.
4. Otherwise, pop the first key from the list and create a nested dictionary based on the remaining keys and the value.
5. Add the nested dictionary to the result dictionary.
6. Call the add_to_dict function recursively with the remaining keys and the nested dictionary.
7. Return the result dictionary.
Python3
def group_hierarchy_split2(test_dict, splt_chr):
result = {}
for key in test_dict:
key_parts = key.split(splt_chr)
inner_dict = add_to_dict(key_parts[1:], test_dict[key])
result.setdefault(key_parts[0], {}).update(inner_dict)
return result
def add_to_dict(keys, value):
if not keys:
return value
key = keys.pop(0)
return {key: add_to_dict(keys, value)}
test_dict = {"1-3" : 2, "8-7" : 0, "1-8" : 10, "8-6" : 15}
splt_chr = "-"
print(group_hierarchy_split2(test_dict, splt_chr))
Output{'1': {'3': 2, '8': 10}, '8': {'7': 0, '6': 15}}
Time complexity: O(nm) where n is the number of keys in the input dictionary and m is the maximum length of a key.
Auxiliary Space: O(nm) where n is the number of keys in the input dictionary and m is the maximum length of a key.
Method 4: Using itertools.groupby()
Step-by-step approach:
Import the groupby function from the itertools module.
Initialize the dictionary test_dict.
Print the original dictionary.
Initialize the split character splt_chr.
Use the groupby() function to group the keys in test_dict based on the first element of each key.
Sort the keys of test_dict before grouping to ensure that the groups are contiguous.
For each group of keys, create a nested dictionary with the second element of each key as the inner key and the corresponding value from test_dict as the inner value.
Use a dictionary comprehension to create a new dictionary with the grouped dictionaries as the inner dictionaries, and the first element of each key as the outer key.
Print the resulting dictionary.
Python3
# importing groupby from itertools
from itertools import groupby
# initializing dictionary
test_dict = {"1-3" : 2, "8-7" : 0, "1-8" : 10, "8-6" : 15}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing split char
splt_chr = "-"
# group the keys based on the first element using groupby()
grouped_keys = groupby(sorted(test_dict.keys()), lambda x: x.split(splt_chr)[0])
# create a new dictionary with nested dictionaries
res = {k: {x.split(splt_chr)[1]: test_dict[x] for x in v} for k, v in grouped_keys}
# printing result
print("The splitted dictionary : " + str(res))
OutputThe original dictionary is : {'1-3': 2, '8-7': 0, '1-8': 10, '8-6': 15}
The splitted dictionary : {'1': {'3': 2, '8': 10}, '8': {'6': 15, '7': 0}}
Time complexity: O(nlogn), where n is the number of key-value pairs in the dictionary due to sorting the keys.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.
Similar Reads
Python - Group Similar keys in dictionary
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
5 min read
Python - Swapping Hierarchy in Nested Dictionaries
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform hierarchy swapping of nested dictionaries. This problem can have application in domains which require dictionary restructuring. Let's discuss certain ways in which this task can be performed. Input :
4 min read
Python - Group list of tuples to dictionary
The task is to convert a list of tuples into a dictionary, where each tuple consists of two elements. The first element of each tuple becomes the key and the second element becomes the value. If a key appears multiple times, its corresponding values should be grouped together, typically in a list.Fo
3 min read
Python | Extract key-value of dictionary in variables
Sometimes, while working with dictionaries, we can face a problem in which we may have just a singleton dictionary, i.e dictionary with just a single key-value pair, and require to get the pair in separate variables. This kind of problem can come in day-day programming. Let's discuss certain ways in
5 min read
Python | Difference in keys of two dictionaries
In this article, we will be given two dictionaries dic1 and dic2 which may contain the same keys and we have to find the difference of keys in the given dictionaries using Python. Example Input: dict1= {'key1':'Geeks', 'key2':'For', 'key3':'geeks'}, dict2= {'key1':'Geeks', 'key2':'Portal'} Output: k
5 min read
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 | Split given dictionary in half
While working with dictionaries, sometimes we might have a problem in which we need to reduce the space taken by single container and wish to divide the dictionary into 2 halves. Let's discuss certain ways in which this task can be performed. Method #1 : Using items() + len() + list slicing The comb
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 | 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
Get Index of Values in Python Dictionary
Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe
3 min read