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 be discussing how to compare elements in two dictionaries in Python. We will go over the syntax for comparing dictionary elements and will provide examples of how to do so.

Dictionaries in Python

In Python, a dictionary can be created by placing a sequence of elements within curly { } brackets , separated by ‘comma’ ( , ). Dictionary holds pairs of values, one being the key and the other corresponding pair element being its value.

Values in a dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated and must be immutable and unique. The name of keys in a dictionary is case sensitive. Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly brackets { }.

We can declare a dictionary in the following way −

thisdict = { "brand": "Ford", "model": "Mustang", year": 1964 }

In this article, we will look how we can compare elements of 2 dictionaries in python using 3 different methods.

Using the equality operator ( = = )

In this method, we will compare two strings using the double equals comparison operator. The == operator returns true when the left hand side and the right hand side of the operators are equals, and it returns false when both of them are not equal.

If the 2 dictionaries given to us are equal and identical to each other, this operator will return true and we can conclude that the two dictionaries are equal. And it will return false if they are not equal.

Example

In the following example, we are using == operator to compare 2 dictionaries

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 ")

Output

The output for the above code will be –

dict1 is not equal to dict2

Using loops to compare two dictionaries

In this method, we will compare the elements of two dictionaries one by one by iterating over the length of one dictionary and checking the key and values for every iteration with the corresponding key and value pair in other dictionary.

We will also check for the length of both the dictionaries, if they are not the same, we can directly conclude that the dictionaries will not be equal. To get the value corresponding to a key in dictionary we use the. get function which gives the value of a key given as argument to the function.

Example

In the following example we will.

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 ")
else:
    flag=0
    for i in dict1:
        if dict1.get(i) != dict2.get(i):
            flag=1
            break
    if flag==0:
        print (" dict1 is equal to dict2 ")
    else:
        print (" dict1 is not equal to dict2 ")

Output

The output for the above program will be as follows −

dict1 is not equal to dict2

Using list comprehension method

In this method, we will use list comprehension to compare two dictionaries. List comprehension is a shorter way to write a for loop in a list, tuple or dictionary. In this method we will iterate through one of the dictionary and compare if the values for same key in both the dictionaries is same or not. If they are same the dictionaries will be equal and not equal of they are not the same.

Example

The below python code shows how we can use list comprehension to compare two given dictionaries and print the result.

dict1 = { 'first' : 'apple' , 'second' : 'orange' , 'third' : 'mango' }
dict2 = { 'first' : 'banana' , 'second' : 'guava' , 'third' : 'grapes' }
ans = all ( dict2.get (key) == value for key , value in dict1.items() )
if ans == 'true':
   print ("dict1 and dict2 are equal")
else:
   print ("dict1 and dict2 are not equal")

Output

The output for the above code will be as follows −

dict1 and dict2 are not equal

Conclusion

In this article, we came to know about dictionaries in python, where we can use dictionaries. We also learnt how we can compare 2 given dictionaries. We came across 3 different methods to compare 2 dictionaries.

The first method involved use of equality operator ( ==). The second method involved use of iteration to check each and every key value pair of both the dictionaries. In the final method we used the list comprehension method of python to iterate over key value pair of one dictionary and check the values for the keys in both dictionaries and compared them.

The time complexity of 1st approach is O (1) as it uses simple comparison. Whereas the other 2 method have time complexity of O (n). where n is the length of dictionary.

Updated on: 17-Feb-2023

805 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements