-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion 22- Dict
33 lines (21 loc) · 1.04 KB
/
Question 22- Dict
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Given a dictionary with nested dictionaries as values, extract all the values with of particular key.
Input : test_dict = {‘sup’ : {“a” : 7, “b” : 9, “c” : 12}, ‘is’ : {“a” : 15, “b” : 19, “c” : 20}, ‘best’ :{“a” : 5, “b” : 10, “c” : 2}}, temp = “b”
Output : [9, 10, 19]
Explanation : All values of “b” key are extracted.
Input : test_dict = {‘sup’ : {“a” : 7, “b” : 9, “c” : 12}, ‘is’ : {“a” : 15, “b” : 19, “c” : 20}, ‘best’ :{“a” : 5, “b” : 10, “c” : 2}}, temp = “a”
Output : [7, 15, 5]
Explanation : All values of “a” key are extracted.
def get_key(input_dict,key):
res=[]
for value in input_dict.values():
if key in value.keys():
res.append(value[key])
return res
if __name__ == "__main__":
input_dict = {
'sup': {"a" : 7, "b" : 9, "c" : 12},
'is' : {"a" : 15, "b" : 19, "c" : 20},
'best':{"a" : 5, "b" : 10, "c" : 2}
}
key = "c"
print(get_key(input_dict,key))