I have inherited Python code which I don't fully understand yet. Here is a structured list which needs only values from the Alternatives key to be returned by a function. The data is like:
{
'Response':
[
{
'SavingsMessage': '',
'SavingNotification':
[
{'AuthorizationNumber': '12345' 'Alternatives': []},
{'AuthorizationNumber': '6666', 'Alternatives': [{'NDC': '99999'} ]
]
}
Above data is in a nested_value variable and is passed to a function in such format:
level_two = get_nested_value_IRFAN([nested_value,*schema[key][level_one_key][level_one_key]])
while the values of the *schema[key][level_one_key][level_one_key]] is:
[0, 'Response', 0, 'SavingNotification', 0, 'Alternatives']
And here is the function itself:
def get_nested_value_IRFAN(path):
return functools.reduce(lambda a,b:a[b],path)
The function should return only the values of the Alternatives key, per my understanding of the code but nothing is returned by the function--just blank [] returns? Or I could use nested_value variable in a different way to extract the values of the Alternative key. I have tried a few things but nothing has worked so far.
Thanks in advance!
0.reduceis probably the wrong thing to use hereget_nested_value_IRFAN()only gets one value from the specified path. You need a loop to get all of them.[0, 'Response', 0, 'SavingNotification']to get to that level, then you can loop and getitem['Alternatives']