
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to find the second maximum value in Dictionary
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given two integers, we need to print the second maximum value in the dictionary
Now let’s observe the concept in the implementation below−
Approach 1 − Using sorted() function by negative indexes
Example
#input example_dict ={"tutor":3, "tutorials":15, "point":9,"tutorialspoint":19} # sorting the given list and get the second last element print(list(sorted(example_dict.values()))[-2])
Output
15
Approach 2 − Here we use sort method on the list and then access the second largest element
Example
list1 = [11,22,1,2,5,67,21,32] # using built-in sort method list1.sort() # second last element print("Second largest element in the list is:", list1[-2])
Output
Second largest element in the list is: 32
Approach 3 − Here we apply the brute-force method without using a built-in function
Example
list1 = [11,22,1,2,5,67,21,32] #assuming max_ is equal to maximum of element at 0th and 1st index and secondmax is the minimum among them max_=max(list1[0],list1[1]) secondmax=min(list1[0],list1[1]) for i in range(2,len(list1)): # if found element is greater than max_ if list1[i]>max_: secondmax=max_ max_=list1[i] #if found element is greator than secondmax else: if list1[i]>secondmax: secondmax=list1[i] print("Second highest number is the list is : ",str(secondmax))
Output
Second highest number is the list is : 32
Conclusion
In this article, we have learned about how we can find the second maximum value in a dictionary ).
- Related Articles
- Swift Program to Find Maximum Key-Value Pair in the Dictionary
- Program to find maximum erasure value in Python
- Get key with maximum value in Dictionary in Python
- Program to find maximum possible value of smallest group in Python
- Python program to get maximum of each key Dictionary List
- Swift Program to Find Minimum Key-Value Pair in the Dictionary
- Python program to Convert Matrix to Dictionary Value List
- Program to find out the cells containing maximum value in a matrix in Python
- Python Program to print key value pairs in a dictionary
- Program to find minimum possible maximum value after k operations in python
- Program to find maximum value by inserting operators in between numbers in Python
- Python program to find the sum of dictionary keys
- Program to find out the maximum value of a 'valid' array in Python
- Python program to find the second largest number in a list
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers

Advertisements