Given a list of dictionaries, with each dictionary having different keys, extract value of key K.
Input : test_list = [{“Gfg” : 3, “b” : 7}, {“is” : 5, ‘a’ : 10}, {“Best” : 9, ‘c’ : 11}], K = ‘b’
Output : 7
Explanation : Value of b is 7.Input : test_list = [{“Gfg” : 3, “b” : 7}, {“is” : 5, ‘a’ : 10}, {“Best” : 9, ‘c’ : 11}], K = ‘c’
Output : 11
Explanation : Value of c is 11.
Method #1 : Using list comprehension
This is one of the ways in which this task can be performed. In this, we iterate for each dictionary inside list, and check for key in it, if present the required value is returned.
Python3
# Python3 code to demonstrate working of # Extract Key's value from Mixed Dictionaries List # Using list comprehension # initializing list test_list = [{ "Gfg" : 3 , "b" : 7 }, { "is" : 5 , 'a' : 10 }, { "Best" : 9 , 'c' : 11 }] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 'Best' # list comprehension to get key's value # using in operator to check if key is present in dictionary res = [sub[K] for sub in test_list if K in sub][ 0 ] # printing result print ( "The extracted value : " + str (res)) |
The original list : [{'Gfg': 3, 'b': 7}, {'is': 5, 'a': 10}, {'Best': 9, 'c': 11}] The extracted value : 9
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using update() + loop
This is yet another way in which this task can be performed. In this, we update each dictionary into each other. Forming one large dictionary, and then the value is extracted from this dictionary.
Python3
# Python3 code to demonstrate working of # Extract Key's value from Mixed Dictionaries List # Using update() + loop # initializing list test_list = [{ "Gfg" : 3 , "b" : 7 }, { "is" : 5 , 'a' : 10 }, { "Best" : 9 , 'c' : 11 }] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 'Best' res = dict () for sub in test_list: # merging all Dictionaries into 1 res.update(sub) # printing result print ( "The extracted value : " + str (res[K])) |
The original list : [{'Gfg': 3, 'b': 7}, {'is': 5, 'a': 10}, {'Best': 9, 'c': 11}] The extracted value : 9
Time complexity: O(n*n), where n is the length of the dictionary list. The update() + loop takes O(n*n) time
Auxiliary Space: O(1), constant extra space is required
Method #3: Using map() + lambda function
We are using the map() function along with a lambda function to extract the value of the key K from each dictionary in the test_list.
First, we use the filter() function to filter out the dictionaries in the test_list that contain the key K. Then, we use the map() function to extract the value of K from each dictionary in the filtered list. Finally, we convert the result of map() into a list and select the first element, which is the value of K in the first dictionary that contains it.
Python3
# Python3 code to demonstrate working of # Extract Key's value from Mixed Dictionaries List # Using map() + lambda function # initializing list test_list = [{ "Gfg" : 3 , "b" : 7 }, { "is" : 5 , 'a' : 10 }, { "Best" : 9 , 'c' : 11 }] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 'Best' # Using map() + lambda function to extract value res = list ( map ( lambda x: x[K], filter ( lambda x: K in x, test_list)))[ 0 ] # printing result print ( "The extracted value : " + str (res)) |
The original list : [{'Gfg': 3, 'b': 7}, {'is': 5, 'a': 10}, {'Best': 9, 'c': 11}] The extracted value : 9
Time complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), where n is the length of the input list.
Method 4: Use a for loop to iterate over each dictionary and check if the key exists in the dictionary
The program first initializes a list of dictionaries and a key K. It then uses filter() and lambda to create a new list containing only the dictionaries that contain the key K. Next, it uses map() and another lambda function to extract the value associated with K from each of these dictionaries. Finally, the program converts the resulting map object to a list and retrieves the first element, which should be the extracted value.
Python3
# Python3 code to demonstrate working of # Extract Key's value from Mixed Dictionaries List # Using for loop # initializing list test_list = [{ "Gfg" : 3 , "b" : 7 }, { "is" : 5 , 'a' : 10 }, { "Best" : 9 , 'c' : 11 }] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 'Best' # Using for loop to extract value res = None for dictionary in test_list: if K in dictionary: res = dictionary[K] break # printing result print ( "The extracted value : " + str (res)) |
The original list : [{'Gfg': 3, 'b': 7}, {'is': 5, 'a': 10}, {'Best': 9, 'c': 11}] The extracted value : 9
The time complexity of this approach is O(n), where n is the number of dictionaries in the list.
The auxiliary space is O(1), as we are not creating any new data structures to store the intermediate results.
Method #5: Using try-except block
This program extracts the value of a given key from a list of dictionaries using the try-except block. It iterates through the list of dictionaries and uses a try-except block to extract the value of the given key. If the key is not present in any of the dictionaries, it returns None.
Python3
# Python3 code to demonstrate working of # Extract Key's value from Mixed Dictionaries List # Using try-except block # initializing list test_list = [{ "Gfg" : 3 , "b" : 7 }, { "is" : 5 , 'a' : 10 }, { "Best" : 9 , 'c' : 11 }] # initializing K K = 'Best' # Using try-except block to extract value res = None for dictionary in test_list: try : res = dictionary[K] except KeyError: pass else : break # printing original list print ( "The original list : " + str (test_list)) # printing result print ( "The extracted value : " + str (res)) |
The original list : [{'Gfg': 3, 'b': 7}, {'is': 5, 'a': 10}, {'Best': 9, 'c': 11}] The extracted value : 9
Time complexity: The time complexity of the above code is O(n), where n is the number of dictionaries in the list.
Auxiliary space: The auxiliary space used by the above code is O(1), as we are using constant space to store the key and value extracted from the list.
Method #6: Using dictionary comprehension
- Create a dictionary comprehension to extract the values of key ‘K’ from each dictionary in the list.
- Use the if statement to check if the key ‘K’ is present in the dictionary.
- Return the value of key ‘K’ if it is present in the dictionary, otherwise return None.
- Convert the resulting dictionary into a list of values.
Python3
# Python3 code to demonstrate working of # Extract Key's value from Mixed Dictionaries List # Using dictionary comprehension # initializing list test_list = [{ "Gfg" : 3 , "b" : 7 }, { "is" : 5 , 'a' : 10 }, { "Best" : 9 , 'c' : 11 }] # initializing K K = 'Best' # Using dictionary comprehension to extract value res_dict = {d.get(K): None for d in test_list if K in d} # Converting dictionary into list of values res_list = [v for v in res_dict if v is not None ] # printing result print ( "The extracted value : " + str (res_list[ 0 ])) |
The extracted value : 9
Time complexity: O(n)
Auxiliary space: O(n)