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
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)])
One possibility is to generate a list of tuples with keys and items of target elements
tmp_tpl = [(i, my_dict[i]) for i in ["x", "y", "z", "w"]]
And then take the max of that:
out = max(tmp_tpl, key= lambda x:x[1])
Note that this will print a tuple of key and item which lets you identify which key did the maximum item correspond to.