
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Difference in keys of two dictionaries
Two python dictionaries may contain some common keys between them. In this article we will find how to get the difference in the keys present in two given dictionaries.
With set
Here we take two dictionaries and apply set function to them. Then we subtract the two sets to get the difference. We do it both ways, by subtracting second dictionary from first and next subtracting first dictionary form second. Those keys which are not common are listed in the result set.
Example
dictA = {'1': 'Mon', '2': 'Tue', '3': 'Wed'} print("1st Distionary:\n",dictA) dictB = {'3': 'Wed', '4': 'Thu','5':'Fri'} print("1st Distionary:\n",dictB) res1 = set(dictA) - set(dictB) res2 = set(dictB) - set(dictA) print("\nThe difference in keys between both the dictionaries:") print(res1,res2)
Output
Running the above code gives us the following result −
1st Distionary: {'1': 'Mon', '2': 'Tue', '3': 'Wed'} 1st Distionary: {'3': 'Wed', '4': 'Thu', '5': 'Fri'} The difference in keys between both the dictionaries: {'2', '1'} {'4', '5'}
Using in with for loop
In another approach we can use a for loop to iterate through the keys of one dictionary and check for its presence using the in clause in the second dictionary.
Example
dictA = {'1': 'Mon', '2': 'Tue', '3': 'Wed'} print("1st Distionary:\n",dictA) dictB = {'3': 'Wed', '4': 'Thu','5':'Fri'} print("1st Distionary:\n",dictB) print("\nThe keys in 1st dictionary but not in the second:") for key in dictA.keys(): if not key in dictB: print(key)
Output
Running the above code gives us the following result −
1st Distionary: {'1': 'Mon', '2': 'Tue', '3': 'Wed'} 1st Distionary: {'3': 'Wed', '4': 'Thu', '5': 'Fri'} The keys in 1st dictionary but not in the second: 1 2
- Related Articles
- How to find difference in keys contained in two Python dictionaries?
- Python - Intersect two dictionaries through keys
- Handling missing keys in Python dictionaries
- Python – Merge Dictionaries List with duplicate Keys
- Python Program to get all unique keys from a List of Dictionaries
- Python program to merge two Dictionaries
- How do we compare two dictionaries in Python?
- Python Program to compare elements in two dictionaries
- How can we merge two Python dictionaries?
- Python Program to Concatenate Two Dictionaries Into One
- How to merge two Python dictionaries in a single expression?
- Flatten given list of dictionaries in Python
- How to compare two dictionaries in C#?
- What are Ordered dictionaries in Python?
- get() method for dictionaries in Python

Advertisements