I am currently using this for loop to sort a dictionary's (used as a hashmap) values in descending order, then append these keys to a list:
hashmap = {1: 2, 2: 3, 3: 1}
ans = []
for v in sorted(hashmap, key=hashmap.get, reverse=True):
ans.append(v)
This does return me the correct answer, however I do not understand the hashmap.get
call.
From what I understand the dict.get
method has the required parameter keyname, and so should cause an error if none is passed. Is it that the keyword key
is automatically extracting all keys from this call?
Thank you for any help.