Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Find the dictionary entry with 2nd highest associated value
- my_dict = {'a': 10, 'c': 30, 'b':20, 'd':40, 'g': 25}
- listv = list(my_dict.values()) # extract a list of value from the dictionary
- listk = list(my_dict.keys()) # extract a list of keys from the dictionary
- # Note: there might be a direct way of extracting value+key pairs, but I've not found it yet
- listboth = list(zip(listv, listk)) # gives a list of tuples, being value,key pairs
- listsorted = sorted(listboth, reverse=True) # this seems to sort by value (1st entry in tuple)
- mytuple = listsorted[1] # extract the tuple for 2nd highest value
- print("2nd largest value is for dictionary key", mytuple[1], "value", mytuple[0])
Add Comment
Please, Sign In to add comment