Python program to convert a matrix into a dictionary.

In this python dictionary program, we will convert a matrix into a dictionary.

Steps to solve the program
  1. Take a matrix as input and create an empty list.
  2. Use for loop with the range function to iterate over the length of the dictionary using len().
  3. Add 1 to the length and assign this as the key and its corresponding index value as its value.
  4. Print the output.
				
					matrix = [[1,2,3],[4,5,6]]
dict1 = {}
for i in range(len(matrix)):
    dict1[i+1] = matrix[i]
    
dict1
				
			

Output :

				
					{1: [1, 2, 3], 2: [4, 5, 6]}
				
			

convert string to the dictionary.

check all values are the same in a dictionary.

Python program to convert string to the dictionary.

In this python dictionary program, we will convert string to the dictionary.

Steps to solve the program
  1. Take a string as input in the given format.
  2. First split the string on then, split the string on =.
  3. Then convert it to the dictionary using dict().
  4. Print the output.
				
					str1 = "Apr=April; Mar=March"
dictionary = dict(subString.split("=") 
                  for subString in str1.split(";"))

print(dictionary)
				
			

Output :

				
					{'Apr': 'April', ' Mar': 'March'}
				
			

convert a list of Tuples into a dictionary

convert a matrix into a dictionary.

Convert a list of Tuples into a dictionary

In this python dictionary program, we will convert a list of Tuples into a dictionary

Steps to solve the program
  1. Take a list of tuples as input and create an empty list.
  2. Use for loop to iterate over a list of tuples.
  3. From tuples add 1st element as the key and 2nd element as its value to the dictionary.
  4. Print the output.
				
					list1 = [("mike",1),("sarah",20),("jim", 16)]
dict1 = {}
for val in list1:
    dict1[val[0]] = val[1]
    
print(dict1)
				
			

Output :

				
					{'mike': 1, 'sarah': 20, 'jim': 16}
				
			

convert a key-values list to a flat dictionary.

convert string to the dictionary.

Convert a key-values list to a flat dictionary

In this python dictionary program, we will convert a key-values list to a flat dictionary.

Steps to solve the program
  1. Take a list having a list of values as input.
  2. Convert a key-values list to a flat dictionary using zip() and dict().

  3. Zip() will convert the list of values into a key-value pair.
  4. Print the output.
				
					dict1 = {'name':['Apr','May','June'],'month':[4, 5, 6]}

res = dict(zip(dict1['name'],dict1['month']))

print(res)
				
			

Output :

				
					{'Apr': 4, 'May': 5, 'June': 6}
				
			

filter a dictionary based on values.

convert a list of Tuples into a dictionary

Python program to filter a dictionary based on values.

In this python dictionary program, we will filter a dictionary based on values.

Steps to solve the program
  1. Take a dictionary as input and create en empty dictionary.
  2. Use for loop to iterate over keys and values of the input dictionary.
  3. Add the key-value pairs in the empty dictionary for which values are greater than 40.
  4. Use a conditional statement for this purpose.
  5. Print the output.
				
					dict1 = {'alex':50,'john':45,'Robert' :30}
dict2 = {}

for key,val in dict1.items():
    if val > 40:
        dict2[key] = val
        
print(dict2)
				
			

Output :

				
					{'alex': 50, 'john': 45}
				
			

drop empty Items from a given dictionary.

convert a key-values list to a flat dictionary.

Python program to drop empty Items from a given dictionary.

In this python dictionary program, we will drop empty Items from a given dictionary.

Steps to solve the program
  1. Take a dictionary as input and create an empty dictionary.
  2. Using a for loop and an if-else statement add the key-value pairs from the input dictionary whose value is not empty to the empty dictionary.
  3. Print the output.
				
					dict1 = {"m1":40,"m2":50,"m3":None}
dict2 = {}
for key,val in dict1.items():
    if val != None:
        dict2[key] = val
        
dict2
				
			

Output :

				
					{'m1': 40, 'm2': 50}
				
			

create a dictionary of keys a, b, and c where each key has as value a list from 1-5, 6-10, and 11-15 respectively. 

filter a dictionary based on values.

Python program to create a dictionary

In this python dictionary program, we will create a dictionary of keys a, b, and c where each key has as value a list from 1-5, 6-10, and 11-15 respectively. 

Steps to solve the program
  1. Create three empty lists and an empty dictionary.
  2. In the first empty list add numbers between 1-5 using a for loop.
  3. Repeat this process for the second and third lists to add respective numbers.
  4. Add the three lists with respective keys to the empty dictionary.
  5. Print the output.
				
					l1= []
for i in range(1,6):
    l1.append(i)
    
l2 = []
for i in range(6,11):
    l2.append(i)
    
l3 = []
for i in range(11,16):
    l3.append(i)
    
dict1 = {}
dict1["a"] = l1
dict1["b"] = l2
dict1["c"] = l3

print(dict1)
				
			

Output :

				
					{'a': [1, 2, 3, 4, 5], 'b': [6, 7, 8, 9, 10], 'c': [11, 12, 13, 14, 15]}
				
			

match key values in two dictionaries.

drop empty Items from a given dictionary.

Python program to match key values in two dictionaries.

In this python dictionary program, we will match key values in two dictionaries.

Steps to solve the program
  1. Take two dictionaries as input.
  2. Use for loop with an if-else statement to check whether keys from the first dictionary exist in the second dictionary or not.
  3. Print the respective output.
				
					dict1 = {'k1':'p','k2':'q','k3':'r'}
dict2 = {'k1':'p','k2':'s'}

for key in dict1:
    if key in dict2:
        print(f"{key} is present in both dictionaries")
    else:
        print(f"{key} is present not in both dictionaries")
				
			

Output :

				
					k1 is present in both dictionaries
k2 is present in both dictionaries
k3 is present not in both dictionaries
				
			

replace dictionary values with their average.

create a dictionary of keys a, b, and c where each key has as value a list from 1-5, 6-10, and 11-15 respectively.

Python program to replace dictionary values with their average.

In this python dictionary program, we will replace dictionary values with their average.

Steps to solve the program
  1. Take a dictionary in a list as input.
  2. Use for loop to iterate over the dictionary.
  3. Create two variables and assign their values equal to the values of the respective keys using pop().
  4. Create a new key and assign its value equal to the average of the above two variables.
  5. Add the above key to the dictionary.
  6. Print the output.
				
					dict1 = [{'name':'ketan','subject':'maths','p1':80,'p2':70}]
for d in dict1:
    n1 = d.pop('p1')
    n2 = d.pop('p2')
    d['p1+p2'] = (n1 + n2)/2
    
print(dict1)
				
			

Output :

				
					[{'name': 'ketan', 'subject': 'maths', 'p1+p2': 75.0}]
				
			

sort items in a dictionary in descending order.

match key values in two dictionaries.

Sort items in a dictionary in descending order.

In this python dictionary program, we will sort items in a dictionary in descending order.

Steps to solve the program
  1. Take a dictionary as input.
  2. Sort the dictionary by values in descending order using sorted().
  3. Use values as the key while sorting using the lamdba function.
  4. Pass values of the dictionary to the lambda function.
  5. To sort the dictionary in descending order assign reverse equal to True.
  6. Print the output.
				
					dict1 = {'Math':70,'Physics':90,'Chemistry':67}

a = sorted(dict1.items(), key = lambda val:val[1],reverse = True)

print(a)
				
			

Output :

				
					[('Physics', 90), ('Math', 70), ('Chemistry', 67)]

				
			

count a number of items in a dictionary value that is in a list.

replace dictionary values with their average.