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
# Python3 code to demonstrate working of
# Extract Unique value key pairs
# Using list comprehension
# initializing list
test_list = [{'gfg' : 5, 'is' : 8, 'best' : 12},
{'gfg' : 5, 'is' : 12, 'best' : 12},
{'gfg' : 20, 'is' : 17, 'best' : 12}]
# printing original list
print("The original list is : " + str(test_list))
# initializing required keys
req_key1 = 'gfg'
req_key2 = 'best'
# Extract Unique value key pairs
# Using list comprehension
res = {tuple(sub[idx] for idx in [req_key1, req_key2]) for sub in test_list}
# printing result
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
# Python3 code to demonstrate working of
# Extract Unique value key pairs
# Using map() + zip() + itemgetter()
from operator import itemgetter
# initializing list
test_list = [{'gfg' : 5, 'is' : 8, 'best' : 12},
{'gfg' : 5, 'is' : 12, 'best' : 12},
{'gfg' : 15, 'is' : 17, 'best' : 21}]
# printing original list
print("The original list is : " + str(test_list))
# initializing required keys
req_key1 = 'gfg'
req_key2 = 'best'
# Extract Unique value key pairs
# Using map() + zip() + itemgetter()
temp = zip(*map(itemgetter(req_key1, req_key2), test_list))
res = list(map(set, temp))
# printing result
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
# Python3 code to demonstrate working of
# Extract Unique value key pairs
# Using a for loop and a set
# initializing list
test_list = [{'gfg' : 5, 'is' : 8, 'best' : 12},
{'gfg' : 5, 'is' : 12, 'best' : 12},
{'gfg' : 20, 'is' : 17, 'best' : 12}]
# printing original list
print("The original list is : " + str(test_list))
# initializing required keys
req_key1 = 'gfg'
req_key2 = 'best'
# Extract Unique value key pairs
# Using a for loop and a set
res_set = set()
for sub in test_list:
res_set.add((sub[req_key1], sub[req_key2]))
res = list(res_set)
# printing result
print("The required values : " + str(res))
OutputThe 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))
OutputThe 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.
Similar Reads
Python - Unique values count of each Key
Given a Dictionaries list, the task is to write a Python program to count the unique values of each key. Example: Input : test_list = [{"gfg" : 1, "is" : 3, "best": 2}, {"gfg" : 1, "is" : 3, "best" : 6}, {"gfg" : 7, "is" : 3, "best" : 10}] Output : {'gfg': 2, 'is': 1, 'best': 3} Explanation : gfg ha
7 min read
Python - Extract Similar Key Values
Given a dictionary, extract all values which are from similar keys, i.e contains all similar characters, just jumbled to form each other. Input : test_dict = {'gfg' : 5, 'ggf' : 19, 'gffg' : 9, 'gff' : 3, 'fgg' : 3}, tst_wrd = 'fgg' Output : [5, 19, 3] Explanation : gfg, ggf and fgg have values, 5,
5 min read
Extract Subset of Key-Value Pairs from Python Dictionary
In this article, we will study different approaches by which we can Extract the Subset Of Key-Value Pairs From the Python Dictionary. When we work with Python dictionaries, it often involves extracting subsets of key-value pairs based on specific criteria. This can be useful for tasks such as filter
4 min read
Key with Maximum Unique Values - Python
The task involves finding the key whose list contains the most unique values in a dictionary. Each list is converted to a set to remove duplicates and the key with the longest set is identified. For example, in d = {"A": [1, 2, 2], "B": [3, 4, 5, 3], "C": [6, 7, 7, 8]}, key "C" has the most unique v
3 min read
Python - Unique Values of Key in Dictionary
We are given a list of dictionaries and our task is to extract all the unique values associated with a given key. For example, consider: data = [ {"name": "Aryan", "age": 25}, {"name": "Harsh", "age": 30}, {"name": "Kunal", "age": 22}, {"name": "Aryan", "age": 27}]key = "name"Then, the unique values
4 min read
Python - Extract Keys with specific Value Type
Given a dictionary, extract all the keys, whose values are of a given type. Input : test_dict = {'gfg' : 2, 'is' : 'hello', 'for' : {'1' : 3}, 'geeks' : 4}, targ_type = int Output : ['gfg', 'geeks'] Explanation : gfg and geeks have integer values. Input : test_dict = {'gfg' : 2, 'is' : 'hello', 'for
7 min read
Python | Unique values in Matrix
Sometimes we need to find the unique values in a list, which is comparatively easy and has been discussed earlier. But we can also get a matrix as input i.e a list of lists, and finding unique in them are discussed in this article. Let's see certain ways in which this can be achieved. Method #1: Usi
9 min read
Python - Extract values of Particular Key in Nested Values
We are given a nested dictionary we need to extract the values of particular key. For example, we are having a dictionary d = {'a': 1,'b': {'a': 2, 'c': 3},'d': [{'a': 4}, {'a': 5}]} we need to to extract values from it. so that output becomes [1, 5, 4, 2]. Using a StackWe can manually manage a stac
3 min read
Python - Tuple key detection from value list
Sometimes, while working with record data, we can have a problem in which we need to extract the key which has matching value of K from its value list. This kind of problem can occur in domains that are linked to data. Lets discuss certain ways in which this task can be performed. Method #1 : Using
6 min read
Python - Extracting keys not in values
Sometimes, while working with Python dictionaries, we can have a problem in which we require to get all the keys that do not occur in values lists. This can have applications in the domains such as day-day programming. Lets discuss certain ways in which this task can be performed. Method #1 : Using
5 min read