
- 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
How do we compare two dictionaries in Python?
dicts in python are also classes. These have the __eq__method overridden, so you can use the == operator to check if 2 dictionaries are equal or not.
example
a = {'foo': 10, 'bar': 150} b = {'foo': 10, 'bar': 150} print(a == b)
Output
This will give the output −
True
If you want a list of shared items in the 2 dictionaries, you can use sets and the & operator on them to get that.
example
a = {'foo': 10, 'bar': 150} b = {'foo': 10, 'baz': 50} shared = set(a.items()) & set(b.items()) print(shared)
Output
This will give the output −
{('foo', 10)}
- Related Articles
- How do we compare two lists in Python?
- How do we compare two tuples in Python?
- Python Program to compare elements in two dictionaries
- How can we merge two Python dictionaries?
- How to compare two dictionaries in C#?
- How do we compare the elements of two lists in Python?
- How do we compare Python Dates?
- How do we compare two arrays in Java?
- How do we compare String in Java
- Python - Difference in keys of two dictionaries
- How to merge two Python dictionaries in a single expression?
- Python program to merge two Dictionaries
- Python - Intersect two dictionaries through keys
- How can we compare data in two MySQL tables?
- How to find difference in keys contained in two Python dictionaries?

Advertisements