Home  >  Article  >  Backend Development  >  Python program to compare elements in two dictionaries

Python program to compare elements in two dictionaries

王林
王林forward
2023-08-20 19:25:061551browse

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.

Dictionary in Python

In Python, a dictionary can be created by placing a sequence of elements within curly braces { }, separated by commas (,). Dictionaries hold key-value pairs, where one is the key and the other is the corresponding 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 see how to compare elements of two dictionaries in Python using 3 different methods.

Use the equal sign operator (= =)

In this method, we will use the double equal sign comparison operator to compare two strings . The == operator returns true when the left and right sides of the operator are equal and false when they 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 example below, we use the == 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

Compare two dictionaries using a loop

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

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

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 of the above program is as follows:

dict1 is not equal to dict2

Use 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 of the above code is 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 learn 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.

The above is the detailed content of Python program to compare elements in two dictionaries. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete