Python – Extract Key’s value from Mixed Dictionaries List
Last Updated :
21 Apr, 2023
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
test_list = [{ "Gfg" : 3 , "b" : 7 },
{ "is" : 5 , 'a' : 10 },
{ "Best" : 9 , 'c' : 11 }]
print ( "The original list : " + str (test_list))
K = 'Best'
res = [sub[K] for sub in test_list if K in sub][ 0 ]
print ( "The extracted value : " + str (res))
|
Output
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
test_list = [{ "Gfg" : 3 , "b" : 7 },
{ "is" : 5 , 'a' : 10 },
{ "Best" : 9 , 'c' : 11 }]
print ( "The original list : " + str (test_list))
K = 'Best'
res = dict ()
for sub in test_list:
res.update(sub)
print ( "The extracted value : " + str (res[K]))
|
Output
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
test_list = [{ "Gfg" : 3 , "b" : 7 },
{ "is" : 5 , 'a' : 10 },
{ "Best" : 9 , 'c' : 11 }]
print ( "The original list : " + str (test_list))
K = 'Best'
res = list ( map ( lambda x: x[K], filter ( lambda x: K in x, test_list)))[ 0 ]
print ( "The extracted value : " + str (res))
|
Output
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
test_list = [{ "Gfg" : 3 , "b" : 7 },
{ "is" : 5 , 'a' : 10 },
{ "Best" : 9 , 'c' : 11 }]
print ( "The original list : " + str (test_list))
K = 'Best'
res = None
for dictionary in test_list:
if K in dictionary:
res = dictionary[K]
break
print ( "The extracted value : " + str (res))
|
Output
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
test_list = [{ "Gfg" : 3 , "b" : 7 },
{ "is" : 5 , 'a' : 10 },
{ "Best" : 9 , 'c' : 11 }]
K = 'Best'
res = None
for dictionary in test_list:
try :
res = dictionary[K]
except KeyError:
pass
else :
break
print ( "The original list : " + str (test_list))
print ( "The extracted value : " + str (res))
|
Output
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
test_list = [{ "Gfg" : 3 , "b" : 7 },
{ "is" : 5 , 'a' : 10 },
{ "Best" : 9 , 'c' : 11 }]
K = 'Best'
res_dict = {d.get(K): None for d in test_list if K in d}
res_list = [v for v in res_dict if v is not None ]
print ( "The extracted value : " + str (res_list[ 0 ]))
|
Output
The extracted value : 9
Time complexity: O(n)
Auxiliary space: O(n)
Similar Reads
Python - Extract Maximum Keys' value dictionaries
Given a dictionary, extract all the dictionary which contains a any key which has maximum values among other keys in dictionary list. Input : [{"Gfg" : 3, "is" : 7, "Best" : 8}, {"Gfg" : 9, "is" : 2, "Best" : 9}, {"Gfg" : 5, "is" : 4, "Best" : 10}, {"Gfg" : 3, "is" : 6, "Best" : 14}] Output : [{"Gfg
6 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
Python - Extract Kth index elements from Dictionary Value list
Given a dictionary with list as values, extract all the Kth index elements. Input : {"Gfg" : [4, 7, 5], "Best" : [8, 6, 7], "is" : [9, 3, 8]}, K = 2 Output : [5, 7, 8] Explanation : The 2nd index elements are 5, 7 and 8 respectively in different keys. Input : {"Gfg" : [4, 7, 5], "Best" : [8, 6, 7],
7 min read
Python - Extract Key's Value, if Key Present in List and Dictionary
Given a list, dictionary and a Key K, print the value of K from the dictionary if the key is present in both, the list and the dictionary. For example: a = ["Gfg", "is", "Good", "for", "Geeks"], d = {"Gfg": 5, "Best": 6} and K = "Gfg" then the output will be 5 as "Gfg" is present in both the list an
3 min read
Python | Extract specific keys from dictionary
We have a lot of variations and applications of dictionary containers in Python and sometimes, we wish to perform a filter of keys in a dictionary, i.e extracting just the keys which are present in the particular container. Let's discuss certain ways in which this can be performed. Extract specific
4 min read
Python | Extract filtered Dictionary Values
While working with Python dictionaries, there can be cases in which we are just concerned about getting the filtered values list and donât care about keys. This is yet another essential utility and solution to it should be known and discussed. Letâs perform this task through certain methods. Method
4 min read
Python | Sum values for each key in nested dictionary
Given a nested dictionary and we have to find sum of particular value in that nested dictionary. This is basically useful in cases where we are given a JSON object or we have scraped a particular page and we want to sum the value of a particular attribute in objects. Code #1: Find sum of sharpness v
2 min read
Get List of Values From Dictionary - Python
We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3]. Using dict.values()We can use dict.values() along with the list() function to get the list. Here,
2 min read
Extract Dictionary Values as a Python List
To extract dictionary values from a list, we iterate through each dictionary, check for the key's presence, and collect its value. The result is a list of values corresponding to the specified key across all dictionaries.For example, given data = {'a': 1, 'b': 2, 'c': 3}, the output will be [1, 2, 3
3 min read
Python - Remove K valued key from Nested Dictionary
We are given a nested dictionary we need to remove K valued key. For example, we are given a nested dictionary d = { "a": 1, "b": {"c": 2,"d": {"e": 3,"f": 1},"g": 1},"h": [1, {"i": 1, "j": 4}]} we need to remove K valued key ( in our case we took k value as 1 ) from it so that the output should be
3 min read