Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Convert dictionary to K sized dictionaries using Python?
Dictionaries are key-value data structures in Python where the keys are unique and values can be repeated. In this article, we will explore how to convert a dictionary into K smaller dictionaries by distributing key-value pairs evenly across K separate dictionaries.
Problem Example
Given a dictionary with 9 key-value pairs:
original_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'x': 8, 'y': 9}
k = 3
# Expected output: 3 dictionaries with distributed pairs
# [{'a': 1, 'd': 4, 'g': 7}, {'b': 2, 'e': 5, 'x': 8}, {'c': 3, 'f': 6, 'y': 9}]
Method 1: Using Modulo Operator (Naive Approach)
The simplest approach distributes items by using the modulo operator to cycle through K dictionaries:
def convert_dict_naive(dictionary, k):
result = [{} for _ in range(k)]
for i, (key, value) in enumerate(dictionary.items()):
index = i % k
result[index][key] = value
return result
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'x': 8, 'y': 9}
k = 3
result = convert_dict_naive(my_dict, k)
print(result)
[{'a': 1, 'd': 4, 'g': 7}, {'b': 2, 'e': 5, 'x': 8}, {'c': 3, 'f': 6, 'y': 9}]
Method 2: Using itertools.cycle
The itertools.cycle() function creates an infinite iterator that cycles through the given sequence. Combined with next(), it provides a clean way to distribute items:
import itertools
def convert_dict_cycle(dictionary, k):
result = [{} for _ in range(k)]
key_cycle = itertools.cycle(range(k))
for key, value in dictionary.items():
index = next(key_cycle)
result[index][key] = value
return result
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'x': 8, 'y': 9}
k = 3
result = convert_dict_cycle(my_dict, k)
print(result)
[{'a': 1, 'd': 4, 'g': 7}, {'b': 2, 'e': 5, 'x': 8}, {'c': 3, 'f': 6, 'y': 9}]
Method 3: Using List Comprehension with Slicing
This method uses list comprehension to create K dictionaries by slicing keys and values with a step of K:
def convert_dict_slicing(dictionary, k):
items = list(dictionary.items())
return [dict(items[start::k]) for start in range(k)]
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'x': 8, 'y': 9}
k = 3
result = convert_dict_slicing(my_dict, k)
print(result)
[{'a': 1, 'd': 4, 'g': 7}, {'b': 2, 'e': 5, 'x': 8}, {'c': 3, 'f': 6, 'y': 9}]
Method 4: Using numpy.array_split
NumPy's array_split() function divides indices into equal groups, which we can use to create consecutive chunks:
import numpy as np
def convert_dict_numpy(dictionary, k):
items = list(dictionary.items())
result = []
for group in np.array_split(range(len(items)), k):
sub_dict = {items[i][0]: items[i][1] for i in group}
result.append(sub_dict)
return result
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'x': 8, 'y': 9}
k = 3
result = convert_dict_numpy(my_dict, k)
print(result)
[{'a': 1, 'b': 2, 'c': 3}, {'d': 4, 'e': 5, 'f': 6}, {'g': 7, 'x': 8, 'y': 9}]
Comparison
| Method | Distribution Pattern | Dependencies | Best For |
|---|---|---|---|
| Modulo Operator | Round-robin | None | Simple, no imports needed |
| itertools.cycle | Round-robin | itertools | Clean, readable code |
| List Slicing | Round-robin | None | Concise one-liner |
| numpy.array_split | Consecutive chunks | numpy | Equal-sized consecutive groups |
Conclusion
All methods efficiently convert a dictionary into K smaller dictionaries with O(n) time complexity. Use the modulo operator for simplicity, itertools.cycle for readability, or numpy.array_split when you need consecutive chunks rather than round-robin distribution.
