For the following definition of a dict:
data={
'key_1':1,
'key_2':2,
'key_3':[
{
'key_4':[4,5],
'key_5':[6,7],
'key_6':8
},
{
'key_4':[9,10],
'key_5':[11,12],
'key_6':13
}
],
'key_7':14
}
I basically need to check whether a given key exists in data or not and if it does, the values associated with the key must be printed.
Example:
input: key_5
output: ([6,7],[11,12])
input: key_8
output: DNE
Code that I wrote:
key = input()
def find_key(data,key):
if key in data:
print(data[key])
return
for k, v in data.items():
if isinstance(v,list):
for x in v:
if isinstance(x,dict) and find_key(x,key) is not None:
print(find_key(x,key))
find_key(data,key)
I'm not sure about where to place the condition of 'DNE' in this code. Can I get some help on this?