Skip to content
Related Articles

Related Articles

Improve Article

Python – Group Hierarachy Splits of keys in Dictionary

  • Last Updated : 09 May, 2021

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 Hierarachy 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))
Output
The 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 #2 : Using defaultdict() 

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 Hierarachy 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)))
Output
The 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}}

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :