Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to compare elements in two dictionaries
Dictionaries are a powerful data type in Python that allow you to store data as key-value pairs. In this article, we will discuss how to compare elements in two dictionaries using three different approaches: equality operator, loops, and list comprehension.
Understanding Dictionaries
In Python, a dictionary is created by placing key-value pairs within curly brackets { }, separated by commas. Values can be of any data type and can be duplicated, whereas keys must be unique and immutable.
# Creating dictionaries
dict1 = {"brand": "Ford", "model": "Mustang", "year": 1964}
dict2 = {"brand": "Toyota", "model": "Camry", "year": 2020}
print("Dictionary 1:", dict1)
print("Dictionary 2:", dict2)
Dictionary 1: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
Dictionary 2: {'brand': 'Toyota', 'model': 'Camry', 'year': 2020}
Method 1: Using the Equality Operator (==)
The == operator compares two dictionaries for complete equality. It returns True if both dictionaries have identical key-value pairs, False otherwise.
dict1 = {'first': 'apple', 'second': 'orange', 'third': 'mango'}
dict2 = {'first': 'apple', 'second': 'orange', 'third': 'grapes'}
if dict1 == dict2:
print("dict1 is equal to dict2")
else:
print("dict1 is not equal to dict2")
# Test with identical dictionaries
dict3 = {'first': 'apple', 'second': 'orange', 'third': 'mango'}
print("dict1 == dict3:", dict1 == dict3)
dict1 is not equal to dict2 dict1 == dict3: True
Method 2: Using Loops for Manual Comparison
This method compares dictionaries by iterating through each key-value pair manually. We first check if both dictionaries have the same length, then compare corresponding values.
dict1 = {'first': 'apple', 'second': 'orange', 'third': 'mango'}
dict2 = {'first': 'banana', 'second': 'guava', 'third': 'grapes'}
if len(dict1) != len(dict2):
print("The dictionaries are not equal (different lengths)")
else:
are_equal = True
for key in dict1:
if dict1.get(key) != dict2.get(key):
are_equal = False
break
if are_equal:
print("dict1 is equal to dict2")
else:
print("dict1 is not equal to dict2")
dict1 is not equal to dict2
Method 3: Using List Comprehension with all()
This approach uses list comprehension with the all() function to check if all key-value pairs match between dictionaries.
dict1 = {'first': 'apple', 'second': 'orange', 'third': 'mango'}
dict2 = {'first': 'banana', 'second': 'guava', 'third': 'grapes'}
# Check if all key-value pairs match
result = all(dict2.get(key) == value for key, value in dict1.items())
if result:
print("dict1 and dict2 are equal")
else:
print("dict1 and dict2 are not equal")
# Test with matching dictionaries
dict3 = {'first': 'apple', 'second': 'orange', 'third': 'mango'}
result2 = all(dict3.get(key) == value for key, value in dict1.items())
print("dict1 and dict3 are equal:", result2)
dict1 and dict2 are not equal dict1 and dict3 are equal: True
Comparison of Methods
| Method | Time Complexity | Best For |
|---|---|---|
| Equality Operator (==) | O(n) | Complete equality check |
| Manual Loop | O(n) | Custom comparison logic |
| List Comprehension | O(n) | Pythonic and concise |
Conclusion
Use the equality operator == for simple dictionary comparison. For custom comparison logic or learning purposes, manual loops provide full control. List comprehension offers a Pythonic approach for complex comparisons.
