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

A dictionary is a unique data structure in Python that stores data as key-value pairs, where each key is a unique identifier used to access its corresponding value. We can perform various operations on dictionaries to manipulate the stored data.

This article explains how to divide a dictionary into K equal dictionaries where each value is divided by K, and K represents the number of keys in the original dictionary.

Understanding the Problem

Given a dictionary, we need to create K copies where each value is divided by K (the total number of keys). Let's understand this with an example ?

# Original dictionary
original_dict = {"Score": 100, "Age": 40, "Salary": 25000, "cutoff": 44}
print("Original:", original_dict)
print("Number of keys (K):", len(original_dict))
Original: {'Score': 100, 'Age': 40, 'Salary': 25000, 'cutoff': 44}
Number of keys (K): 4

The expected output would be 4 dictionaries, each containing all keys with values divided by 4.

Method 1: Using Iterations

This approach iterates through the dictionary, divides each value by K, and creates K identical copies ?

def divide_dict_iteration(input_dict):
    K = len(input_dict)
    result_list = []
    
    # Create K identical dictionaries
    for i in range(K):
        divided_dict = {}
        for key in input_dict:
            divided_dict[key] = input_dict[key] / K
        result_list.append(divided_dict)
    
    return result_list

# Example usage
original_dict = {"Score": 100, "Age": 40, "Salary": 25000, "cutoff": 44}
print("Original dictionary:", original_dict)

result = divide_dict_iteration(original_dict)
print("Divided dictionaries:")
for i, d in enumerate(result):
    print(f"Dictionary {i+1}: {d}")
Original dictionary: {'Score': 100, 'Age': 40, 'Salary': 25000, 'cutoff': 44}
Divided dictionaries:
Dictionary 1: {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}
Dictionary 2: {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}
Dictionary 3: {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}
Dictionary 4: {'Score': 25.0, 'Age': 10.0, 'Salary': 6250.0, 'cutoff': 11.0}

Method 2: Using Dictionary and List Comprehension

This optimized approach uses comprehensions to create the divided dictionary and replicate it K times ?

def divide_dict_comprehension(input_dict):
    K = len(input_dict)
    
    # Create divided dictionary using dictionary comprehension
    divided_dict = {key: value/K for key, value in input_dict.items()}
    
    # Create list of K identical dictionaries using list comprehension
    result_list = [divided_dict for _ in range(K)]
    
    return result_list

# Example usage
cricket_stats = {"Sixes": 244, "Fours": 528, "Strike_rate": 164, "Balls_faced": 864}
print("Original dictionary:", cricket_stats)

result = divide_dict_comprehension(cricket_stats)
print("Number of dictionaries created:", len(result))
print("Each dictionary:", result[0])
Original dictionary: {'Sixes': 244, 'Fours': 528, 'Strike_rate': 164, 'Balls_faced': 864}
Number of dictionaries created: 4
Each dictionary: {'Sixes': 61.0, 'Fours': 132.0, 'Strike_rate': 41.0, 'Balls_faced': 216.0}

Comparison

Method Code Length Readability Performance
Iterations Longer More explicit Slower
Comprehensions Shorter More Pythonic Faster

Practical Example

Here's a complete example demonstrating both methods ?

def compare_methods(test_dict):
    print(f"Input dictionary: {test_dict}")
    K = len(test_dict)
    print(f"K value (number of keys): {K}")
    
    # Method 1: Iterations
    result1 = []
    for i in range(K):
        temp_dict = {key: value/K for key, value in test_dict.items()}
        result1.append(temp_dict)
    
    # Method 2: Comprehensions
    divided = {key: value/K for key, value in test_dict.items()}
    result2 = [divided for _ in range(K)]
    
    # Verify both methods produce same result
    print("Both methods produce identical results:", result1 == result2)
    print("Sample output:", result1[0])

# Test with different dictionary
test_data = {"A": 20, "B": 30, "C": 40}
compare_methods(test_data)
Input dictionary: {'A': 20, 'B': 30, 'C': 40}
K value (number of keys): 3
Both methods produce identical results: True
Sample output: {'A': 6.666666666666667, 'B': 10.0, 'C': 13.333333333333334}

Conclusion

Both methods successfully divide a dictionary into K equal parts. The iteration method is more explicit and easier to understand, while the comprehension method is more Pythonic and efficient. Choose the comprehension approach for better performance and cleaner code.

Updated on: 2026-03-27T07:34:16+05:30

550 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements