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


A dictionary is a unique form of array that is used to implement data structures in python. There are several characteristics associated with a dictionary that makes it a very powerful tool in Python. It stores data in the form of key-value pair where each key is a unique identifier and is used to access the corresponding value associated with it.

We can perform several operations on this dictionary and manipulate the data stored in it. This article will explain one such operation where we will divide a dictionary and its key into K equal dictionaries.

Understanding the Problem

We have to pass a dictionary and then divide it into K number of equal dictionaries, where “K” is the size of the original dictionary. The division should be in a manner that it divides all the keys equally. Let’s understand this with the help of an example −

Input: dict1 = {"Score":100, "Age": 40, "Salary": 25000, "cutoff":44}
Output: [{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, 
{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, 
{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, 
{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}]

Here each value associated with different keys is reduced to 1/K times the original value and a list of K dictionaries are returned. Now that we have discussed the problem statement, let’s discussed a few solutions.

Using Iterations

In this approach, we will pass a sample dictionary and then obtain the “K” value with the help of “len()” method. This method will return the length of the dictionary. After this, we will iterate over the sample dictionary and divide each “Key value” by K with the help of “/” operand.

We will store these divided values in an empty dictionary and then add all the newly created dictionaries in an empty list with the help of “append()” method.

Example

dict1 = {"Score":100 , "Age": 40, "Salary": 25000, "cutoff":44}
print(f"Original dictionary is: {dict1}")
K = len(dict1)
print(f"The value for K is: {K}")

empLis = []
empDict ={}
for keys in dict1:
   empDict[keys] = dict1[keys]/K
   empLis.append(empDict)

print(f"The newly divided dictionary is: {empLis}")

Output

Original dictionary is: {'Score': 100, 'Age': 40, 'Salary': 25000, 'cutoff': 44}
The value for K is: 4
The newly divided dictionary is: [{'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}, {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}]

Using List Comprehension and Dictionary Comprehension

This approach is an optimized version of the previous solution. Here we will summarize the iterations in a single dictionary and list with the help of dictionary comprehension and list comprehension. After passing the sample dictionary, we will create a dictionary in which we will store the divided values (DivDict).

The iteration takes place and it returns keys from the original dictionary divided by K. A list (lisDict) stores the K number of dictionaries containing the divided values. We assign the length of the list equal to the K value.

Example

dict1 = {"Number of sixes":244, "Number of fours": 528, "Strike rate": 164, "Balls faced":864}
print(f"Original dictionary is: {dict1}")
K = len(dict1)
print(f"The value for K is: {K}")

#using dictionary comprehension
DivDict ={key_value:dict1[key_value]/K for key_value in dict1}
#using list comprehension
lisDict = [DivDict for i in range(K)]

print(f"The newly divided dictionary is: {lisDict}")

Output

Original dictionary is: {'Number of sixes': 244, 'Number of fours': 528, 'Strike rate': 164, 'Balls faced': 864}
The value for K is: 4
The newly divided dictionary is: [{'Number of sixes': 61.0, 'Number of fours': 132.0, 'Strike rate': 41.0, 'Balls faced': 216.0}, {'Number of sixes': 61.0, 'Number of fours': 132.0, 'Strike rate': 41.0, 'Balls faced': 216.0}, {'Number of sixes': 61.0, 'Number of fours': 132.0, 'Strike rate': 41.0, 'Balls faced': 216.0}, {'Number of sixes': 61.0, 'Number of fours': 132.0, 'Strike rate': 41.0, 'Balls faced': 216.0}]

There are other methods that involve the use of methods like: - zip(), lambda(), groupby(), slicing etc.

These methods can be used when we have to introduce certain specifications into the code such as targeting specific values or keys in a dictionary. The above-discussed solutions are the basic approaches that can be used to split up a sample dictionary into K equal parts.

Conclusion

During the course of this article, we discussed two solutions to divide a dictionary and its keys into K equal dictionaries. The 1st solution revolved around the “looping concept” where we iterated through the dictionary and added it into a list. The 2nd solution focused on a more optimized approach where we summarized the entire looping concept into a single dictionary and list.

Updated on: 12-Jul-2023

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements