In this python dictionary program, we will match key values in two dictionaries.
Steps to solve the program
- Take two dictionaries as input.
- Use for loop with an if-else statement to check whether keys from the first dictionary exist in the second dictionary or not.
- 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