How to retrieve the nth largest value in Python

Since it is used relatively for competitive programming, it is only a memorandum. There are 2 patterns

1. Including duplication

Input

numbers = [9,2,5,4,7]
numbers.sort()
print(numbers)
print(numbers[-2])

Output

[2,4,5,7,9]
7

2. Do not include duplicates

Input

numbers = [8, 8, 1, 4, 3]
a = sorted(set(numbers))
print(a)
print(a[-2])

Output

[1 ,3 ,4 ,8]
4

Recommended Posts

How to retrieve the nth largest value in Python
How to get the last (last) value in a list in Python
How to use the C library in Python
How to get the files in the [Python] folder
How to develop in Python
How to get the variable name itself in python
How to get the number of digits in Python
How to know the current directory in Python in Blender
How to use the model learned in Lobe in Python
How to retrieve multiple arrays using slice in python.
[Python] How to output the list values in order
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
How to get the Python version
[Python] How to import the library
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
[python] How to check if the Key exists in the dictionary
How to debug the Python standard library in Visual Studio
How to use the __call__ method in a Python class
Get index of nth largest / smallest value in list in Python
Get index of nth largest / smallest value in list in Python
[Introduction to Python] How to use class in Python?
How to determine the existence of a selenium element in Python
How to access environment variables in Python
How to know the internal structure of an object in Python
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
The 15th offline real-time how to write reference problem in Python
The 17th Offline Real-time How to Solve Writing Problems in Python
How to check the memory size of a variable in Python
How to judge that the cross key is input in Python3
How to work with BigQuery in Python
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
How to get a stacktrace in python
Use numpy's .flatten () [0] to retrieve the value
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to check if the contents of the dictionary are the same in Python by hash value
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
The 14th offline real-time how to write reference problem in python
How to use regular expressions in Python
Solve the smallest value in Python (equivalent to paiza rank D)
How to display Hello world in python
[Beginner memo] How to specify the library reading path in Python
How to use is and == in Python
How to write Ruby to_s in Python
What to do when the value type is ambiguous in Python?