Tuesday, 7 July 2026

πŸš€ Day 83/150 – Find Common Keys in Dictionaries in Python

 

πŸš€ Day 83/150 – Find Common Keys in Dictionaries in Python

Dictionaries are one of Python's most powerful data structures. Sometimes you need to compare two dictionaries and identify the keys they have in common. Python provides several easy and efficient ways to accomplish this.

In this post, we'll explore four different methods to find common keys between dictionaries.


Method 1 – Using Set Intersection (&)

The easiest way is to compare the dictionary keys using the intersection operator.

dict1 = {"name": "John", "age": 20, "city": "Delhi"} dict2 = {"age": 25, "city": "Mumbai", "country": "India"} common = dict1.keys() & dict2.keys() print(common)







Output:
{'age', 'city'}
Explanation:
  • keys() returns a view of dictionary keys.
  • The & operator finds keys present in both dictionaries.

Method 2 – Using intersection() Method

You can also use the intersection() method for better readability.

dict1 = {"a": 1, "b": 2, "c": 3} dict2 = {"b": 5, "c": 7, "d": 9} common = dict1.keys().intersection(dict2.keys()) print(common)




Output:

{'b', 'c'}

Explanation:
  • intersection() performs the same operation as &.
  • It returns a set of common keys.

Method 3 – Using a For Loop

Loop through one dictionary and check whether each key exists in the other.


dict1 = {"x": 10, "y": 20, "z": 30} dict2 = {"y": 100, "z": 200, "a": 300} for key in dict1: if key in dict2: print(key)






Output:
y
z

Explanation:

  • Iterate over the first dictionary.
  • Print keys that also exist in the second dictionary.

Method 4 – Taking User Input

Compare two user-defined dictionaries.

dict1 = {"apple": 5, "banana": 3, "mango": 7} dict2 = {"banana": 10, "orange": 4, "apple": 2} common = dict1.keys() & dict2.keys() print("Common Keys:", common)




Output:

Common Keys: {'apple', 'banana'}

Explanation:
  • Works with any dictionaries.
  • Returns only the keys that appear in both.

Comparison of Methods

MethodBest For
Set Intersection (&)Fastest and shortest
intersection()Readable code
For LoopLearning and custom logic
User DictionaryReal-world dictionary comparison

πŸ”₯ Key Takeaways

✅ Dictionary keys can be compared using set operations.

✅ The & operator is the shortest and fastest way to find common keys.

✅ intersection() offers the same functionality with clearer syntax.

✅ A for loop is useful when additional conditions or processing are required.

✅ Finding common keys is useful in data comparison, configuration matching, and API response validation.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (301) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (12) BI (10) Books (274) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (9) Data Analysis (39) Data Analytics (26) data management (16) Data Science (386) Data Strucures (23) Deep Learning (189) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (75) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (339) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1400) Python Coding Challenge (1183) Python Mathematics (4) Python Mistakes (51) Python Quiz (562) Python Tips (22) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)