2

I have a dictionary: D = {'N':5, 'S':0, 'W':6, 'E':1} and I want to get key with maximum value among D['N'] and D['S'].

For example I tried code print(lambda k: max(k['N'], k['S'])(k=D.keys())) but it returns lambda object like this <function <lambda> at 0x000002C7B060C1E0>. Although I wanna get N in output.

Need help. Thanks!

mind-protector
  • 248
  • 2
  • 12

1 Answers1

5

Just pass dict.get function as your key parameter in max():

# to find the max of entire dictionary

max(D, key=D.get)
# 'W'

# to find individual keys

max(['N', 'S'], key=D.get)
# 'N'
r.ook
  • 13,466
  • 2
  • 22
  • 39