English

Write a Python program to find the highest 2 values in a dictionary. - Computer Science (Python)

Advertisements
Advertisements

Question

Write a Python program to find the highest 2 values in a dictionary.

Answer in Brief

Solution

Program:
#Write a Python program to find the highest 2 values in a dictionary
#Defining a dictionary
dic = {"A":12,"B":13,"C":9,"D":89,"E":34,"F":17,"G":65,"H":36,"I":25,"J":11}

#Creating an empty list to store all the values of the dictionary
lst = list()

#Looping through each values and storing it in list
for a in dic.values():
    lst.append(a)

#Sorting the list in ascending order, it will store the highest value at the last index
lst.sort()

#Printing the highest and second highest value using negative indexing of the list
print("Highest value:",lst[-1])
print("Second highest value:",lst[-2])

OUTPUT:
Highest value: 89
Second highest value: 65
shaalaa.com
Introduction to Dictionaries
  Is there an error in this question or solution?
Chapter 10: Tuples and Dictionaries - Exercise [Page 225]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 10 Tuples and Dictionaries
Exercise | Q 3. | Page 225
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×