Python Practice › Dictionary Programs › Program 48
Matching key-value pairs
Write a Python program to find key-value pairs present in both dictionaries.
Input
{"k1": "p", "k2": "q", "k3": "r"}, {"k1": "p", "k2": "s"}Output
k1: p is present in both a and b
Approach
items()views support&.
Solution
a = {"k1": "p", "k2": "q", "k3": "r"}b = {"k1": "p", "k2": "s"}for k, v in a.items() & b.items(): print(f"{k}: {v} is present in both a and b")Output
k1: p is present in both a and b
Learn the concept
New to this topic? Read the lesson, then come back.