w3resource

Python: 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 

Pictorial Presentation:

Python Dictionary: Match key values in two dictionaries.

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: Write a Python program to store a given dictionary in a json file.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Free up Memory:

  • Manual garbage collection can be performed on timely or event based mechanism.
import gc
collected_objects = gc.collect()