In this python dictionary program, we will filter a dictionary based on values.
Steps to solve the program
- Take a dictionary as input and create en empty dictionary.
- Use for loop to iterate over keys and values of the input dictionary.
- Add the key-value pairs in the empty dictionary for which values are greater than 40.
- Use a conditional statement for this purpose.
- 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}