Open In App

Python – Extract Equal Pair Dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

While working with a Python dictionary, we are supposed to create a new dictionary of the existing dictionary having tuple as key. We desire to create a singleton key dictionary with keys only where both elements of pair are equal. This can have applications in many domains. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using loop

This is a brute way in which this task can be performed. In this, we iterate for each element of tuple dictionary and compare for equality to create new dictionary. 

Python3




# Python3 code to demonstrate working of
# Extract Equal Pair Dictionary
# Using loop
 
# initializing dictionary
test_dict = {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
 
# printing original dictionary
print("The original dictionary is : "
      + str(test_dict))
 
# Extract Equal Pair Dictionary
# Using loops
res = dict()
for key, val in test_dict.items():
    if key[0] == key[1]:
        res[key[0]] = val
 
# printing result
print("The dictionary after equality testing : "
      + str(res))


Output

The original dictionary is : {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
The dictionary after equality testing : {1: 4, 3: 7, 2: 11}

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #2 : Using dictionary comprehension:

This is yet another way in which this task can be performed. In this, we use dictionary comprehension instead of the loop to provide shorthand. 

Python3




# Python3 code to demonstrate working of
# Extract Equal Pair Dictionary
# Using dictionary comprehension
 
# initializing dictionary
test_dict = {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Extract Equal Pair Dictionary
# Using dictionary comprehension
res = {idx[0]: j for idx, j in test_dict.items() if idx[0] == idx[1]}
 
# printing result
print("The dictionary after equality testing : " + str(res))


Output

The original dictionary is : {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
The dictionary after equality testing : {1: 4, 3: 7, 2: 11}

Method #3 : Using count() and len() methods

Python3




# Python3 code to demonstrate working of
# Extract Equal Pair Dictionary
# Using loop
 
# initializing dictionary
test_dict = {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
 
# printing original dictionary
print("The original dictionary is : "+ str(test_dict))
 
# Extract Equal Pair Dictionary
# Using loops
res = dict()
for i in list(test_dict.keys()):
    if(i.count(i[0])==len(i)):
        res[i[0]]=test_dict[i]
# printing result
print("The dictionary after equality testing : "+ str(res))


Output

The original dictionary is : {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
The dictionary after equality testing : {1: 4, 3: 7, 2: 11}

Method #4 : Using keys(),tuple() and len() methods

Python3




# Python3 code to demonstrate working of
# Extract Equal Pair Dictionary
# Using loop
 
# initializing dictionary
test_dict = {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
 
# printing original dictionary
print("The original dictionary is : "+ str(test_dict))
 
# Extract Equal Pair Dictionary
# Using loops
res = dict()
for i in list(test_dict.keys()):
    x=[i[0]]*len(i)
    x=tuple(x)
    if(x==i):
        res[i[0]]=test_dict[i]
# printing result
print("The dictionary after equality testing : "+ str(res))


Output

The original dictionary is : {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
The dictionary after equality testing : {1: 4, 3: 7, 2: 11}

Method 4 : using a for loop with conditional statements. 

The steps to implement this method are as follows:

Initialize an empty dictionary to store the equal pair dictionaries.
Loop through each item in the test_dict using a for loop.
Check if the first index of the key tuple is equal to the second index of the key tuple.
If the condition is true, add the key-value pair to the new dictionary.
Print the new dictionary.

Python3




# Python3 code to demonstrate working of
# Extract Equal Pair Dictionary
# Using for loop
 
# initializing dictionary
test_dict = {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Extract Equal Pair Dictionary
# Using for loop
equal_dict = {}
for key, value in test_dict.items():
    if key[0] == key[1]:
        equal_dict[key[0]] = value
 
# printing result
print("The dictionary after equality testing : " + str(equal_dict))


Output

The original dictionary is : {(1, 1): 4, (2, 3): 6, (3, 3): 7, (5, 2): 10, (2, 2): 11}
The dictionary after equality testing : {1: 4, 3: 7, 2: 11}

The time complexity of this method is O(n) as it requires looping through each item in the dictionary. 

The space complexity is also O(n) as it requires creating a new dictionary to store the results.



Last Updated : 25 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads