Given a Dictionary, divide dictionary into K sized different dictionaries list.
Input : test_dict = {‘Gfg’ : 1, ‘is’ : 2, ‘best’ : 3, ‘for’ : 4, ‘geeks’ : 5, ‘CS’ : 6}, K = 3
Output : [{‘Gfg’: 1, ‘is’: 2, ‘best’: 3}, {‘for’: 4, ‘geeks’: 5, ‘CS’: 6}]
Explanation : Divided into size of 3 keys.Input : test_dict = {‘Gfg’ : 1, ‘is’ : 2, ‘best’ : 3, ‘for’ : 4}, K = 2
Output : [{‘Gfg’: 1, ‘is’: 2}, {‘best’: 3, ‘for’: 4}]
Explanation : Divided into size of 2 keys.
Method : Using loop
In this, we iterate for all the keys in dictionary using loop and bifurcate according to size and append to new list.
Python3
# Python3 code to demonstrate working of # Convert dictionary to K Keys dictionaries # Using loop # initializing dictionary test_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'geeks' : 5, 'CS' : 6} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # initializing K K = 2 res = [] count = 0flag = 0indict = dict() for key in test_dict: indict[key] = test_dict[key] count += 1 # checking for K size and avoiding empty dict using flag if count % K == 0 and flag: res.append(indict) # reinitializing dictionary indict = dict() count = 0 flag = 1 # printing result print("The converted list : " + str(res)) |
The original dictionary is : {'Gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'geeks': 5, 'CS': 6}
The converted list : [{'Gfg': 1, 'is': 2}, {'best': 3, 'for': 4}, {'geeks': 5, 'CS': 6}]
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.
Recommended Posts:
- Python - Convert Dictionaries List to Order Key Nested dictionaries
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python - Convert Flat dictionaries to Nested dictionary
- Python - Convert list of dictionaries to Dictionary Value list
- Python program to convert String to K sized Numerical Rows
- Python | Split dictionary of lists to list of dictionaries
- Python | Removing dictionary from list of dictionaries
- Python | Remove duplicate dictionaries from nested dictionary
- Python - Remove dictionary from a list of dictionaries if a particular value is not present
- Python program to divide dictionary and its keys into K equal dictionaries
- Python - Convert String to Nested Dictionaries
- Python - Convert List of Dictionaries to List of Lists
- Python - Convert List to List of dictionaries
- Python - Convert String to List of dictionaries
- Python program to Convert Matrix to List of dictionaries
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python - Incremental K sized Row Matrix Initialization
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

