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.

Leave a Comment