Python Forum
need to compare 2 values in a nested dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: need to compare 2 values in a nested dictionary (/thread-41223.html)



need to compare 2 values in a nested dictionary - jss - Nov-29-2023

i have a nested dictionary like below
from nested_lookup import nested_lookup

sample_dict = {
    "data": {
        "systemdata": [
            {
                "system": "1",
                "tempData": {
                    "temperature": "49C",
                    "temperatureThreshold": "70C"
                }
            },

            {
                "system": "2",
                "tempData": {
                    "temperature": None,
                    "temperatureThreshold": "-1C"
                },

            }
        ],
        "detaildata": [
            {
                "detail": "1",
                "tempData": {
                    "temperature": "42C",
                    "temperatureThreshold": "70C"
                }},
            {
                "detail": "2",
                "tempData": {
                    "temperature": "38C",
                    "temperatureThreshold": "60C"
                },

            }
        ]
    }
}
a = nested_lookup("temperature", sample_dict)
print(a)
b = nested_lookup("temperatureThreshold", sample_dict)
print(b)
i need to return True only if temperature <temperatureThreshold
i tried using nested lookup but i find it tough to compare the values as nested lookup returns a list of the values like below
Output:
['49C', None, '42C', '38C'] ['70C', '-1C', '70C', '60C']
how do i proceed?


RE: need to compare 2 values in a nested dictionary - Gribouillis - Nov-30-2023

(Nov-29-2023, 08:30 PM)jss Wrote: i need to return True only if temperature <temperatureThreshold
The output that you want for the entire example dictionary is not clear. Can you explain it?


RE: need to compare 2 values in a nested dictionary - Pedroski55 - Nov-30-2023

Slight problem, you can't do this:

None < -1
You will get an error.

Also, all values are strings, you will need to deal with that if you want to compare the numerical values.

I would collect the values you want as a list of tuples, then work on that. Never heard of nested_lookup!

sample_dict = {
    "data": {
        "systemdata": [
            {
                "system": "1",
                "tempData": {
                    "temperature": "49C",
                    "temperatureThreshold": "70C"
                }
            },
 
            {
                "system": "2",
                "tempData": {
                    "temperature": None,
                    "temperatureThreshold": "-1C"
                },
 
            }
        ],
        "detaildata": [
            {
                "detail": "1",
                "tempData": {
                    "temperature": "42C",
                    "temperatureThreshold": "70C"
                }},
            {
                "detail": "2",
                "tempData": {
                    "temperature": "38C",
                    "temperatureThreshold": "60C"
                },
 
            }
        ]
    }
}

mydict = sample_dict['data']
mykeys = list(mydict.keys()) # ['systemdata', 'detaildata']
tups = []
for key in mykeys:    
    mylist = mydict[key]
    print('The data is here in mylist[i][tempData]', mylist)
    print(f'mylist is {len(mylist)} long')
    for i in range(len(mylist)):
        tempdict = mylist[i]['tempData']
        print('tempData is', tempdict)
        tup = (tempdict['temperature'], tempdict['temperatureThreshold'])
        tups.append(tup)

for t in tups:
    print(t)
The tups are:

Output:
('49C', '70C') (None, '-1C') ('42C', '70C') ('38C', '60C')