Python program to convert a dictionary into n sized dictionary

Python program to convert a dictionary into n sized dictionary,  In this dictionary program, will break the dictionary into a specified size and store it in a list.

Steps to solve the program

1. Initiate three variables
    N = 3 (size of the dictionary)
    temp = {} (empty dictionary)
    count = 1 (initiate counter)
    output = [] (empty list to store output)
2. Iterate through the dictionary data using the loop.
   
for val in Input_values
3. Store key value in temp dictionary its size is equal to N, and
   add temp to the output list, re-initiate after that  count=1 . and temp={}
4. Print output.

				
					input_dict = { 'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6 }
N = 3
temp = {}
count = 1
output = []
for val in input_dict:
    if count == N:
        output.append(temp)
        count = 1
        temp = {}
    else:
        temp[val] = input_dict[val]
        count+= 1
print("Output : ", output)
				
			

Output :

				
					Output :  [{'a': 1, 'b': 2}, {'d': 4, 'e': 5}]
				
			

Sort Dictionary by values summation

Leave a Comment