-2

My goal is to do the following: Evaluate the existing dictionary using [key(string), item(int), format] and find the item in the dictionary with highest value in key-value pair.

Output the corresponding item (i.e. key value) with the highest value

For example consider the following code:

emails={}
emails={'abc@abc.org':1, 'bcd@bcd.org':2, 'efg@efg.org':3'}

The output should be, in the above example, ('efg@efg.org', 3)

Thank you for your time.

Ryan R.
  • 1
  • 2
  • 1
    You can also evaluate by both: ```for k,v in emails.items():``` k being keys and v being values. – Grzegorz Skibinski Jan 26 '20 at 19:09
  • What is your rule to find the max value? How do you say a key, value pair is highest? – J Arun Mani Jan 26 '20 at 19:10
  • Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – Grzegorz Skibinski Jan 26 '20 at 19:13
  • _If I evaluate by value, I get the highest value as output. If I evaluate by item, I get the item pair based on the evaluation of the string portion of the key. If I evaluate by key, I get the key output based upon evaluation of the string._ That doesn't tell us anything about how you want to sort the items. I think your question could do with some editing, it's a soup of 'item' and 'key'. – AMC Jan 26 '20 at 20:33
  • @AMC I agree. thank you. I tried to make it less of a word soup – Ryan R. Jan 26 '20 at 20:44

2 Answers2

1

If you want the max evaluated by value then you can do

>>> max(emails.items(), key=lambda x:x[1])
('efg@efg.org', 3)
abc
  • 11,579
  • 2
  • 26
  • 51
0

This solution finds the key with the maximum value; you can then access the dictionary with that key if you want the (key, value) pair.

>>> k = max(emails, key=emails.get)
>>> (k, emails[k])
('efg@efg.org', 3)
kaya3
  • 47,440
  • 4
  • 68
  • 97