Python – Extract Unique value key pairs
Last Updated :
06 Apr, 2023
Sometimes, while working on Python dictionaries, we can have a problem in which we need to perform the extraction of selected pairs of keys from dictionary list, that too unique. This kind of problem can have application in many domains including day-day programming. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [{‘gfg’ : 5, ‘best’ : 12}, {‘gfg’ : 5, ‘best’ : 12}, ]
Output : {(5, 12)}
Input : test_list = [{‘gfg’ : 5, ‘is’: 5, ‘best’ : 12}]
Output : {(5, 12)}
Method #1 : Using list comprehension This problem can be solved using above functionality. In this, we perform the match, using conditionals and “in” operator. The whole logic compiled using list comprehension.
Python3
test_list = [{ 'gfg' : 5 , 'is' : 8 , 'best' : 12 },
{ 'gfg' : 5 , 'is' : 12 , 'best' : 12 },
{ 'gfg' : 20 , 'is' : 17 , 'best' : 12 }]
print ("The original list is : " + str (test_list))
req_key1 = 'gfg'
req_key2 = 'best'
res = { tuple (sub[idx] for idx in [req_key1, req_key2]) for sub in test_list}
print ("The required values : " + str (res))
|
Output :
The original list is : [{‘gfg’: 5, ‘is’: 8, ‘best’: 12}, {‘gfg’: 5, ‘is’: 12, ‘best’: 12}, {‘gfg’: 20, ‘is’: 17, ‘best’: 12}] The required values : {(5, 12), (20, 12)}
Time complexity: O(n), where n is the number of dictionaries in the test_list. The code iterates over each dictionary once.
Auxiliary Space: O(n), where n is the number of dictionaries in the test_list. The space used by the res set is proportional to the number of dictionaries in test_list.
Method #2 : Using map() + zip() + itemgetter() The combination of above functionalities can be used to perform this task. In this, we use itemgetter to extract the values, zip() is used to combine values, and map() is used to convert the combined result to set.
Python3
from operator import itemgetter
test_list = [{ 'gfg' : 5 , 'is' : 8 , 'best' : 12 },
{ 'gfg' : 5 , 'is' : 12 , 'best' : 12 },
{ 'gfg' : 15 , 'is' : 17 , 'best' : 21 }]
print ("The original list is : " + str (test_list))
req_key1 = 'gfg'
req_key2 = 'best'
temp = zip ( * map (itemgetter(req_key1, req_key2), test_list))
res = list ( map ( set , temp))
print ("The required values : " + str (res))
|
Output :
The original list is : [{‘gfg’: 5, ‘is’: 8, ‘best’: 12}, {‘gfg’: 5, ‘is’: 12, ‘best’: 12}, {‘gfg’: 15, ‘is’: 17, ‘best’: 21}] The required values : [{5, 15}, {12, 21}]
Time complexity :- O(N)
Space complexity :- O(1)
Method #3: Using a for loop and a set
use a for loop and a set to extract unique key-value pairs from the input list.
Python3
test_list = [{ 'gfg' : 5 , 'is' : 8 , 'best' : 12 },
{ 'gfg' : 5 , 'is' : 12 , 'best' : 12 },
{ 'gfg' : 20 , 'is' : 17 , 'best' : 12 }]
print ( "The original list is : " + str (test_list))
req_key1 = 'gfg'
req_key2 = 'best'
res_set = set ()
for sub in test_list:
res_set.add((sub[req_key1], sub[req_key2]))
res = list (res_set)
print ( "The required values : " + str (res))
|
Output
The original list is : [{'gfg': 5, 'is': 8, 'best': 12}, {'gfg': 5, 'is': 12, 'best': 12}, {'gfg': 20, 'is': 17, 'best': 12}]
The required values : [(5, 12), (20, 12)]
Time complexity: O(n), where n is the number of elements in the input list.
Space complexity: O(n), where n is the number of unique key-value pairs.
Method #4: Using dictionary comprehension and set conversion
- Initialize an empty dictionary and set.
- Use dictionary comprehension to iterate over each dictionary in the list, get the values for the required keys, and add them as key-value pairs to the dictionary. Since the keys of a dictionary are unique, this will automatically remove any duplicates.
- Convert the keys of the dictionary to a set to remove duplicates.
- Convert the set to a list to get the final result.
Python3
test_list = [{ 'gfg' : 5 , 'is' : 8 , 'best' : 12 }, { 'gfg' : 5 , 'is' : 12 , 'best' : 12 }, { 'gfg' : 20 , 'is' : 17 , 'best' : 12 }]
req_key1 = 'gfg'
req_key2 = 'best'
result_dict = {(d[req_key1], d[req_key2]): None for d in test_list}
result_set = set (result_dict.keys())
result = list (result_set)
print ( "The required values : " + str (result))
|
Output
The required values : [(5, 12), (20, 12)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n) for the dictionary and set.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...