Python – Extract Key’s Value, if Key Present in List and Dictionary
Given a list, dictionary and a Key K, print value of K from dictionary if key present in both, list and dictionary.
Input : test_list = ["Gfg", "is", "Good", "for", "Geeks"], test_dict = {"Gfg" : 5, "Best" : 6}, K = "Gfg" Output : 5 Explanation : "Gfg" is present in list and has value 5 in dictionary. Input : test_list = ["Good", "for", "Geeks"], test_dict = {"Gfg" : 5, "Best" : 6}, K = "Gfg" Output : None Explanation : "Gfg" not present in List.
Method #1 : Using all() + generator expression
The combination of the above functions offers one of the ways in which this problem can be solved. In this, we use all() to check for occurrence in both dictionary and list. If the result is true value is extracted from to result.
Python3
# Python3 code to demonstrate working of # Extract Key's Value, if Key Present in List and Dictionary # Using all() + list comprehension # initializing list test_list = [ "Gfg" , "is" , "Good" , "for" , "Geeks" ] # initializing Dictionary test_dict = { "Gfg" : 2 , "is" : 4 , "Best" : 6 } # initializing K K = "Gfg" # printing original list and Dictionary print ( "The original list : " + str (test_list)) print ( "The original Dictionary : " + str (test_dict)) # using all() to check for occurrence in list and dict # encapsulating list and dictionary keys in list res = None if all (K in sub for sub in [test_dict, test_list]): res = test_dict[K] # printing result print ( "Extracted Value : " + str (res)) |
Output
The original list : ['Gfg', 'is', 'Good', 'for', 'Geeks'] The original Dictionary : {'Gfg': 2, 'is': 4, 'Best': 6} Extracted Value : 2
Method #2 : Using set() + intersection()
This is another way to check for the key’s presence in both the containers. In this, we compute the intersection of all values of list and dict keys and test for the Key’s occurrence in that.
Python3
# Python3 code to demonstrate working of # Extract Key's Value, if Key Present in List and Dictionary # Using set() + intersection() # initializing list test_list = [ "Gfg" , "is" , "Good" , "for" , "Geeks" ] # initializing Dictionary test_dict = { "Gfg" : 2 , "is" : 4 , "Best" : 6 } # initializing K K = "Gfg" # printing original list and Dictionary print ( "The original list : " + str (test_list)) print ( "The original Dictionary : " + str (test_dict)) # conversion of lists to set and intersection with keys # using intersection res = None if K in set (test_list).intersection(test_dict): res = test_dict[K] # printing result print ( "Extracted Value : " + str (res)) |
Output
The original list : ['Gfg', 'is', 'Good', 'for', 'Geeks'] The original Dictionary : {'Gfg': 2, 'is': 4, 'Best': 6} Extracted Value : 2
Method #3: Using in operator
Python3
# Python3 code to demonstrate working of # Extract Key's Value, if Key Present in List and Dictionary # initializing list test_list = [ "Gfg" , "is" , "Good" , "for" , "Geeks" ] # initializing Dictionary test_dict = { "Gfg" : 2 , "is" : 4 , "Best" : 6 } # initializing K K = "Gfg" # printing original list and Dictionary print ( "The original list : " + str (test_list)) print ( "The original Dictionary : " + str (test_dict)) if K in test_dict.keys() and K in test_list: res = test_dict[K] # printing result print ( "Extracted Value : " + str (res)) |
Output
The original list : ['Gfg', 'is', 'Good', 'for', 'Geeks'] The original Dictionary : {'Gfg': 2, 'is': 4, 'Best': 6} Extracted Value : 2
Method 4: Using operator.countOf() method
Python3
# Python3 code to demonstrate working of # Extract Key's Value, if Key Present in List and Dictionary import operator as op # initializing list test_list = [ "Gfg" , "is" , "Good" , "for" , "Geeks" ] # initializing Dictionary test_dict = { "Gfg" : 2 , "is" : 4 , "Best" : 6 } # initializing K K = "Gfg" # printing original list and Dictionary print ( "The original list : " + str (test_list)) print ( "The original Dictionary : " + str (test_dict)) if op.countOf(test_dict.keys(), K) > 0 and op.countOf(test_list, K) > 0 : res = test_dict[K] # printing result print ( "Extracted Value : " + str (res)) |
Output
The original list : ['Gfg', 'is', 'Good', 'for', 'Geeks'] The original Dictionary : {'Gfg': 2, 'is': 4, 'Best': 6} Extracted Value : 2
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...