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

Python program to sort dictionary by value summation

Python program to sort dictionary by value summation,  In this dictionary program will sort the dictionary data as per the sum of list data in ascending order.

Steps to solve the program

1. Create an empty dictionary temp, temp = {}
2. Iterate over the dictionary key and value with for loop.
    for key, value in input_dict.items()
3. Add the key and sum of values in the temp dictionary.
    temp[key] = sum(value)
4. Sort all the elements of the dictionary using the sorted method.
    which will return the list tuple of key and value pairs. 
    sorted(temp.items(), key=lambda x:x[1])
5. Convert a list of data into a dictionary.
    output = dict(sorted_values)
6. print the output.

				
					input_dict = { "x":[1,5,6], "y":[4,8,2], "c":[3,9] }
temp = {}
for key, value in input_dict.items():
    temp[key] = sum(value)

sorted_values = sorted(temp.items(), key=lambda x:x[1])
output = dict(sorted_values)
print(output)
				
			

Output :

				
					 { x:12, c:12, y:14}
				
			

Invert a given dictionary with non-unique hashable values.

convert a dictionary into n sized dictionary.

Python program to get the sum of unique elements from dictionary list values.

In this python dictionary program, we will get the sum of unique elements from dictionary list values.

Steps to solve the program
  1. Take a dictionary with the value list as input and create an empty list.
  2. Use for loop to iterate over the value list of the dictionary.
  3. Add unique elements from the value list to the empty list using a nested for loop and an if statement.
  4. Get the sum of the list using sum().
  5. Print the output.
				
					D1 = {'a':[6,7,2,8,1],'b':[2,3,1,6,8,10],'d':[1,8,2,6,9]}
l = []

for v in D1.values():
    for num in v:
        if num not in l:
            l.append(num)
            
print("Sum: ",sum(l))
				
			

Output :

				
					Sum:  46
				
			

reverse each string value in the dictionary and add an underscore before and after the Keys.

Reverse string value in the dictionary and add an underscore before and after the Keys.

In this python dictionary program, we will reverse string value in the dictionary and add an underscore before and after the Keys.

Steps to solve the program
  1. Take a dictionary with string values as input and create an empty dictionary.
  2. Use for loop to iterate over keys and values of the dictionary.
  3. Add an underscore before and after the key using string concatenation.
  4. Add that key to the empty dictionary and its value also.
  5. Print the output.
				
					D1 = {'a':'Python','b':'Programming','c':'Learning'}
D2 = {}

for k,v in D1.items():
    D2['_'+k+'_'] = v[::-1]
    
print(D2)
				
			

Output :

				
					{'_a_': 'nohtyP', '_b_': 'gnimmargorP', '_c_': 'gninraeL'}
				
			

sum unique elements from dictionary list values.

Python program to invert a dictionary with non-unique hashable values.

In this python dictionary program, we will invert a dictionary with non-unique hashable values.

Steps to solve the program
  1. Take a dictionary as input.
  2. From collections import defaultdict. (The Python defaultdict type behaves almost exactly like a regular Python dictionary, but if you try to access or modify a missing key, then defaultdict will automatically create the key and generate a default value for it)
  3. Take a variable and assign its value equal to defaultdict(list).
  4. Use for loop to iterate over keys and values of the input dictionary.
  5. Add the same values with the different keys as keys and their keys as values to that variable to that variable.
  6. Print the output.
				
					from collections import defaultdict
D1 = {'alex':1,'bob':2,'martin':1,'robert':2}
obj = defaultdict(list)

for key, value in D1.items():
    obj[value].append(key)
    
print(obj)
				
			

Output :

				
					defaultdict(<class 'list'>, {1: ['alex', 'martin'], 2: ['bob', 'robert']})
				
			

rename key of a dictionary

Sort Dictionary by values summation

Python program to rename key of a dictionary

In this python dictionary program, we will rename key of a dictionary

Steps to solve the program
  1. Take a dictionary as input.
  2. Add a new key and assign its value to the previous key using pop().
  3. In this way, we will rename the key for that value.
  4. Print the output.
				
					D1 = {'a':19,'b': 20,'c':21,'d':20}
D1['e'] = D1.pop('d')
        
print(D1)
				
			

Output :

				
					{'a': 19, 'b': 20, 'c': 21, 'e': 20}
				
			

delete a list of keys from a dictionary.

Invert a given dictionary with non-unique hashable values.

Python program to delete a list of keys from a dictionary.

In this python dictionary program, we will delete a list of keys from a dictionary.

Steps to solve the program
  1. Take a dictionary and a list of keys as input.
  2. Use for loop to iterate over keys in the list.
  3. Delete the keys from the diction using del.
  4. Print the output.
				
					D1 = {'a':19,'b': 20,'c':21,'d':20,'e':50}
Key = ['a','d','e']

for key in Key:
    del D1[key]
    
print(D1)
				
			

Output :

				
					{'b': 20, 'c': 21}
				
			

initialize a dictionary with default values.

rename key of a dictionary

Python program to initialize a dictionary with default values

In this python dictionary program, we will Initialize a dictionary with default values

Steps to solve the program
  1. Take a list of keys as input and create a dictionary to set the default values for those keys.
  2. Create an empty list.
  3. Use for loop to iterate over keys in the list and add those keys to the empty dictionary and set up their values as default values using another dictionary that we have created.
				
					Name = ['Virat','Rohit']
Defaults = {'sport':'cricket','salary':100000}
D1 = {}

for name in Name:
    D1[name] = Defaults
    
print(D1)
				
			

Output :

				
					{'Virat': {'sport': 'cricket', 'salary': 100000}, 'Rohit': {'sport': 'cricket', 'salary': 100000}}
				
			

create a flat list of all the values in a flat dictionary. 

delete a list of keys from a dictionary.

Create a list of all the values in a dictionary. 

In this python dictionary, we will create a flat list of all the values in a flat dictionary. 

Steps to solve the program
  1. Take a dictionary as input and create an empty list.
  2. Use for loop to iterate over values of the dictionary.
  3. Add the values to the list using the append() function.
  4. Print the output.
				
					D1 = {'sqa':1,'tools':2,'is':3,'best':4} 
l = []

for k in D1.values():
    l.append(k)
    
print(l)
				
			

Output :

				
					[1, 2, 3, 4]
				
			

create a flat list of all the keys in a flat dictionary.

initialize a dictionary with default values

Create a list of all the keys in a dictionary.

In this python dictionary program, we will create a flat list of all the keys in a flat dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty list.
  2. Use for loop to iterate over keys of the dictionary.
  3. Add the keys to the list using the append() function.
  4. Print the output.
				
					D1 = {'sqa':[1,2,5,6],'tools':[3,8,9],'is':[2,5,0],'best':[3,6,8]} 
l = []

for k in D1.keys():
    l.append(k)
    
print(l)
				
			

Output :

				
					['sqa', 'tools', 'is', 'best']
				
			

convert given a dictionary to a list of tuples.

create a flat list of all the values in a flat dictionary.