d = {
"local": {
"count": 1,
"health-beauty": {
"count": 1,
"tanning": {"count": 1}
}
},
"nationwide": {"count": 9.0},
"travel": {"count": 0}
}
In this instance "nationwide"
is the largest.
Code is below to make it easier to attach to scripts:
d = {'travel': {'count': 0}, 'local': {'count': 1, 'health-beauty': {'count': 1, 'tanning': {'count': 1}}}, 'nationwide': {'count': 9.0}}
>>> max(d, key=lambda x: d[x]['count'])
'nationwide'
This should work for nested dictionary:
def find_max(d, name=None):
return max((v, name) if k == "count" else find_max(v, k) for k, v in d.items())
>>> find_max(d)
(9.0, 'nationwide')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With