Python program to divide dictionary and its keys into K equal dictionaries

Given a dictionary, divide it into an equal dictionary list of size k, by dividing each key’s value.
Input : test_dict = {“Gfg” : 9, “is” : 6, “best” : 12} , K = 3
Output : [{‘Gfg’: 3.0, ‘is’: 2.0, ‘best’: 4.0}, {‘Gfg’: 3.0, ‘is’: 2.0, ‘best’: 4.0}, {‘Gfg’: 3.0, ‘is’: 2.0, ‘best’: 4.0}]
Explanation : Values distributed equally among dictionaries.Input : test_dict = {“Gfg” : 6, “is” : 4, “best” : 2} , K = 2
Output : [{‘Gfg’: 3.0, ‘is’: 2.0, ‘best’: 1.0}, {‘Gfg’: 3.0, ‘is’: 2.0, ‘best’: 1.0}]
Explanation : Values distributed equally among dictionaries.
Method #1: Using loop
This is one of the ways in which this task can be performed. In this, we compute each required key’s value and append each dictionary into the dictionaries list.
Python3
# Python3 code to demonstrate working of# Divide dictionary into K equal dictionaries# Using loop# initializing dictionarytest_dict = {"Gfg": 20, "is": 36, "best": 100}# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# initializing sizeK = 4# constructing new dicttemp = dict()for key in test_dict: temp[key] = test_dict[key] / 4# creating listres = []for idx in range(K): res.append(temp)# printing resultprint("Required dictionary list : " + str(res)) |
Output:
The original dictionary is : {‘Gfg’: 20, ‘is’: 36, ‘best’: 100}
Required dictionary list : [{‘Gfg’: 5.0, ‘is’: 9.0, ‘best’: 25.0}, {‘Gfg’: 5.0, ‘is’: 9.0, ‘best’: 25.0}, {‘Gfg’: 5.0, ‘is’: 9.0, ‘best’: 25.0}, {‘Gfg’: 5.0, ‘is’: 9.0, ‘best’: 25.0}]
Method #2 : Using dictionary comprehension + list comprehension
This is yet another way in which this task can be performed. In this, we form dictionary using dictionary comprehension and list using list comprehension as a shorthand to solve this problem.
Python3
# Python3 code to demonstrate working of# Divide dictionary into K equal dictionaries# Using dictionary comprehension + list comprehension# function to divide dictionary# and keys into K equal dictionariesdef divideDictKeys(dictionary, K): # constructing new dict # using dictionary comprehension temp = {key: test_dict[key] / K for key in test_dict} # creating list # using list comprehension res = [temp for idx in range(K)] return str(res)# driver code# initializing dictionarytest_dict = {"Gfg": 20, "is": 36, "best": 100}# initializing sizeK = 4# printing original dictionaryprint("The original dictionary is : " + str(test_dict))# printing resultprint("Required dictionary list : " + divideDictKeys(test_dict, K)) |
Output:
The original dictionary is : {‘Gfg’: 20, ‘is’: 36, ‘best’: 100}
Required dictionary list : [{‘Gfg’: 5.0, ‘is’: 9.0, ‘best’: 25.0}, {‘Gfg’: 5.0, ‘is’: 9.0, ‘best’: 25.0}, {‘Gfg’: 5.0, ‘is’: 9.0, ‘best’: 25.0}, {‘Gfg’: 5.0, ‘is’: 9.0, ‘best’: 25.0}]
The time complexity of this approach is O(n), where n is the size of the dictionary.
The space complexity of this approach is O(n*K), as it creates K new dictionaries of size n/K each.


