w3resource

Python Exercise: Match key values in two dictionaries

Python dictionary: Exercise-38 with Solution

Write a Python program to match key values in two dictionaries.

Sample Solution:-

Python Code:

x = {'key1': 1, 'key2': 3, 'key3': 2}
y = {'key1': 1, 'key2': 2}
for (key, value) in set(x.items()) & set(y.items()):
    print('%s: %s is present in both x and y' % (key, value))
	

Sample Output:

key1: 1 is present in both x and y 

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to replace dictionary values with their sum.
Next: Python Tuple Exercise Home

What is the difficulty level of this exercise?



Inviting useful, relevant, well-written and unique guest posts