find max , min of dict


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #http://selinap.com/2008/08/find-max-or-min-in-python-dictionary/
  2.  
  3. mydict = {'a': 2, 'c': 5, 'b': 1, 'd': 4}
  4. max(mydict, key = lambda x: mydict.get(x) )
  5. min(mydict, key = lambda x: mydict.get(x) )
  6.  
  7. # if multiple key have max value, it'will only return one key

URL: http://selinap.com/2008/08/find-max-or-min-in-python-dictionary/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.