I'm querying a public endpoint to get an exchange rate of varies exchanges that returns a list with nested dictionaries. I'm most interesting in is the key "amount" field in the nested dictionaries. I'm struggling to come up with a solution to store the nested dictionary that has the most "amount" value in a variable. Any ideas would be extremely helpful. I'm banging my head against the wall on this one.
Here is the list:
list_with_nested_dicts = [{"partner":"simpleswap","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"simpleswap","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"stealthex","amount":37.90346104,"currency":"cel","supportRate":2,"duration":66.62083333333334,"fixed":true,"min":39.91443225,"max":2550.215226,"exists":true,"id":""},{"partner":"stealthex","amount":37.20972396,"currency":"cel","supportRate":2,"duration":23.158333333333335,"fixed":false,"min":77.82938688,"max":25209.49720665,"exists":true,"id":""},{"partner":"godex","amount":0,"currency":"cel","supportRate":0,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"changenow","amount":37.2077365,"currency":"cel","supportRate":2,"duration":11.08859649122807,"fixed":false,"min":56.60646516,"max":25259.88795509,"exists":true,"id":""},{"partner":"changelly","amount":0,"currency":"cel","supportRate":1,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"changelly","amount":0,"currency":"cel","supportRate":1,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"instaswap","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"exolix","amount":0,"currency":"cel","supportRate":0,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"fixedfloat","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"switchain","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"changehero","amount":0,"currency":"cel","supportRate":3,"duration":56.41111111111111,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"changehero","amount":0,"currency":"cel","supportRate":3,"duration":2.4833333333333334,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"binance","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"nexchange","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"letsexchange","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""},{"partner":"letsexchange","amount":0,"currency":"cel","supportRate":2,"duration":0,"fixed":false,"min":0,"max":0,"exists":false,"id":""},{"partner":"alfacash","amount":0,"currency":"cel","supportRate":3,"duration":0,"fixed":true,"min":0,"max":0,"exists":false,"id":""}]
The dictionary that has the most amount is:
{"partner":"stealthex","amount":37.90346104,"currency":"cel","supportRate":2,"duration":66.62083333333334,"fixed":true,"min":39.91443225,"max":2550.215226,"exists":true,"id":""}
CodePudding user response:
Use the key
parameter of max
:
from operator import itemgetter
res = max(list_with_nested_dicts, key=itemgetter("amount"))
print(res)
Output
{'partner': 'stealthex', 'amount': 37.90346104, 'currency': 'cel', 'supportRate': 2, 'duration': 66.62083333333334, 'fixed': True, 'min': 39.91443225, 'max': 2550.215226, 'exists': True, 'id': ''}
From the documentation:
The key argument specifies a one-argument ordering function like that used for list.sort()
Note there is no need to use operator.itemgetter
, you could simply use a lambda function as the value of key
:
max(list_with_nested_dicts, key=lambda x: x["amount"])
CodePudding user response:
Your dict is not a valid python expression, e.g., true
is not defined (unless you have defined it before). You might want to replace true
with True
and false
with False
.
You can use max
with key
parameter:
from operator import itemgetter
d = max(list_with_nested_dicts, key=itemgetter('amount'))
print(d) # 083333333334, 'fixed': True, 'min': 39.91443225, 'max': 2550.215226, 'exists': True, 'id': ''}
CodePudding user response:
Try this: Bu you will need to replace 'true' with True and 'false' with 'False' for this code to work.
list_with_nested_dicts.sort(key=lambda a: a['amount'])
print(list_with_nested_dicts[-1])