In this article, we will learn how can we print common keys from two Python dictionaries. We will see that there are multiple methods one can do this task. From brute force solutions of nested loops to optimize ones that trade between space and time to decrease the time complexity we will see different methods to do the task at hand and why they work.
Using Nested Loop to get Common Keys from two Dictionaries in Python
We can find Common Keys from two Dictionaries through nested loops. One of the outer loops will be for one dictionary and the other will be for the second dictionary. In this way, we will be able to check for each possible pair of keys. If one of the dictionaries contains m keys and the second one contains n keys then the time complexity and the space complexity of this method will be:
Time Complexity: O(M x N) Space Complexity: O(1)
a = { "a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 }
b = { "c" : 3 , "d" : 4 , "e" : 5 , "f" : 6 }
for key in a:
if key in b:
print (key, end = " " )
|
c d
Using & operator to get Common Keys from two Dictionaries in Python
There are some assumptions for this method that is the size of the two dictionaries must be the same and the if the keys are same then the value associated with them must be same as well as this method will not work.
We can find Common Keys from two Dictionaries through & operator. In this method, we will iterate through the values assigned to each of the keys and perform an operation between the items of the two dictionaries. In this way, for the same items, their keys will be the same.
a = { "a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 }
b = { "c" : 3 , "d" : 4 , "e" : 5 , "f" : 6 }
res = a.items() & b.items()
for i in sorted (res):
print (i[ 0 ], end = " " )
|
c d
The time complexity of this program is O(n * log(n)), where n is the total number of items in both dictionaries. This is because the items() method returns a set-like object containing a view of the dictionary’s items, and the & operator performs a set intersection operation, both of which have a time complexity of O(n * log(n)). The sorted() function also has a time complexity of O(n * log(n)).
The space complexity is O(n), where n is the total number of items in both dictionaries. This is because the result of the items() method and the set intersection operation is stored in a new set-like object, which requires space proportional to the number of items in both dictionaries.
Using Set’s Intersection() to get Common Keys from two Dictionaries in Python
Python set intersection() method returns a new set with an element that is common to all sets. The intersection of two given sets is the largest set, which contains all the elements that are common to both sets.
a = { "a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 }
b = { "c" : 3 , "d" : 4 , "e" : 5 , "f" : 6 }
common_keys = set (a).intersection(b)
for key in sorted (common_keys):
print (key, end = " " )
|
c d
Time complexity: O(nlogn), where n is the total number of items in both dictionaries.
Auxiliary space: O(n)
Using Dictionary’s Get() to get Common Keys from two Dictionaries in Python
Python Dictionary get() Method return the value for the given key if present in the dictionary. If not, then it will return None (if get() is used with only one argument).
a = { "a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 }
b = { "c" : 3 , "d" : 4 , "e" : 5 , "f" : 6 }
for key in a:
name = b.get(key, None )
if name:
print (key, end = " " )
|
c d
Time complexity: O(n), where n is the total number of items in both dictionaries.
Auxiliary space: O(1)
Filter the keys of dictionary using a lambda function:
To find the common keys between two dictionaries using lambda expressions, we can use the filter() function. The filter() function takes a function and a list as arguments and returns a list containing only the elements for which the function returned True.
Here is an example of how to use filter() to find the common keys between two dictionaries:
a = { "a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 }
b = { "c" : 3 , "d" : 4 , "e" : 5 , "f" : 6 }
common_keys = filter ( lambda x: x in a, b)
for key in sorted (common_keys):
print (key, end = " " )
|
c d
This will print the common keys between the dictionaries a and b, which are c and d.
Time complexity: O(nlogn), where n is the total number of items in both dictionaries.
Auxiliary space: O(n)