1
banks = {
    "National Bank of Canada" : "327",
    "Toronto-Dominion Bank" : "302", 
    "Royal Bank of Canada" : "173", 
    "Wells Fargo" : "273", 
    "Goldman Sachs" : "87", 
    "Morgan Stanley" : "72", 
    "Canadian Imperial Bank of Commerce" : "83",
    "TD Bank" : "108", 
    "Bank of Montreal" : "67", 
    "Capital One" : "47", 
    "FNB Corporation" : "4", 
    "Laurentian Bank of Canada" : "3", 
    "Ally Financial" : "12",
    "Montreal Trust Company" : "145",
    "Canadian Western Bank" : ".97"
}



for value in banks.values():
    count += 1
    total_mkt_cap += float(value)
    total =+ count
    if float(value) > float(largest):
        largest = value

The variable largest is now the value 327, but I would like largest to be the key which is "National Bank of Canada", not the value, any help appreciated, thank you

Krunal Sonparate
  • 1,122
  • 10
  • 29
Tom Weekes
  • 37
  • 3
  • 1
    Does this answer your question? [Getting key with maximum value in dictionary?](https://stackoverflow.com/questions/268272/getting-key-with-maximum-value-in-dictionary) – Yoshikage Kira May 19 '21 at 04:25

2 Answers2

0

Considering that largest contains the largest value, you can find the key associated with it by iterating over the dictionary (key, value):

for key, value in banks.items():
    if value == largest:
        desired_key = key
        break

print(desired_key)

Keep in mind that it will give you the first key associated with this value.

Alternative solution

Alternatively, you can use the following logic that uses operator.itemgetter and ast.literal_eval:

import operator
import ast

banks = {key: ast.literal_eval(value) for key, value in banks.items()}
print(max(banks.items(), key=operator.itemgetter(1))[0])

The previous code will give you the key of the highest value once it has been converted to either an int or a float.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • Thank you so much to both of you! First time actually asking my own question on stackoverflow and you guys answered within minutes. I didn't realise you could iterate over the key and value in the same loop, so that solved it for me, much appreciated – Tom Weekes May 19 '21 at 04:33
  • @TomWeekes, I am glad my answer helped you. If you do not mind, could you please validate my answer? – lmiguelvargasf May 19 '21 at 04:40
  • @Imiguelvargasf I clicked the tick next to your answer now it is green, is that what you meant? – Tom Weekes May 19 '21 at 04:42
  • Your syntax seems not correct, what if you use `elif float(value) >= 10 and float(value) < 300`? – lmiguelvargasf May 19 '21 at 05:12
0

I'd give something like below a try.

banks = {
    "National Bank of Canada" : "327",
    "Toronto-Dominion Bank" : "302", 
}
#don't worry about this line. It just gets the largest value.
largest = str(max(map(int,banks.values())))
for bankName,id in banks.items():  # for name, age in dictionary.iteritems():  (for Python 2.x)
    if id == largest:
        largest = bankName
print(largest)

output

National Bank of Canada
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44