What's the best way to get the biggest value of only the keys x, y, w and z in the dictionary above using Python?
my_dict = {"a":0, "b":5, "c":3, "x":4, "y":3, "w":2, "z": 1}
Thanks
You can try this:
my_dict = {"a":0, "b":5, "c":3, "x":4, "y":3, "w":2, "z": 1}
print(max(my_dict[i] for i in ["x", "y", "w", "z"]))
Output:
4
You could create a temporary dictionary to hold your values, then search in this dictionary for the value you want.
my_dict = {"a":0,"b":5,"c":3,"x":4, "y":3, "w":2, "z": 1}
newdict = {key:my_dict[key] for key in ['x', 'y','w','z']}
print(newdict[max(newdict, key=newdict.get)])
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