Given a list of dictionaries, check whether particular key-value pair exists or not.
Input : [{“Gfg” : “4”, “is” : “good”, “best” : “1”}, {“Gfg” : “9”, “is” : “CS”, “best” : “10”}], K = “Gfg”, val = “find”
Output : False
Explanation : No value of “Gfg” is “find”.Input : [{“Gfg” : “4”, “is” : “good”, “best” : “1”}, {“Gfg” : “9”, “is” : “CS”, “best” : “10”}], K = “Gfg”, val = 4
Output : True
Explanation : 4 present as “Gfg” value.
Method #1 : Using list comprehension
This is one of the ways in which this task can be performed. In this, we extract dictionaries using list comprehension and then use “in” operator to check if it has any values in it.
Python3
# Python3 code to demonstrate working of # Check if particular value is present corresponding to K key # Using list comprehension # initializing lists test_list = [{ "Gfg" : "4" , "is" : "good" , "best" : "1" }, { "Gfg" : "find" , "is" : "better" , "best" : "8" }, { "Gfg" : "9" , "is" : "CS" , "best" : "10" }] # printing original list print ( "The original list : " + str (test_list)) # initializing K key K = "Gfg" # initializing target value val = "find" # extracting values using list comprehension # using in operator to check for values res = val in [sub[K] for sub in test_list] # printing result print ( "Is key-val pair present? : " + str (res)) |
The original list : [{'Gfg': '4', 'is': 'good', 'best': '1'}, {'Gfg': 'find', 'is': 'better', 'best': '8'}, {'Gfg': '9', 'is': 'CS', 'best': '10'}] Is key-val pair present? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using map() + in operator
This is yet another way in which this task can be performed. In this task of getting values corresponding to a particular key is performed using map(), extending function to each dictionary.
Python3
# Python3 code to demonstrate working of # Check if particular value is present corresponding to K key # Using map() + in operator # initializing lists test_list = [{ "Gfg" : "4" , "is" : "good" , "best" : "1" }, { "Gfg" : "find" , "is" : "better" , "best" : "8" }, { "Gfg" : "9" , "is" : "CS" , "best" : "10" }] # printing original list print ( "The original list : " + str (test_list)) # initializing K key K = "Gfg" # initializing target value val = "find" # extracting values using map # using in operator to check for values res = val in list ( map ( lambda sub : sub[K], test_list)) # printing result print ( "Is key-val pair present? : " + str (res)) |
The original list : [{'Gfg': '4', 'is': 'good', 'best': '1'}, {'Gfg': 'find', 'is': 'better', 'best': '8'}, {'Gfg': '9', 'is': 'CS', 'best': '10'}] Is key-val pair present? : True
Time Complexity: O(n) where n is the number of elements in the list “test_list”. This is because we’re using the built-in map() + in operator both has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), no extra space is required