Problem Formulation and Solution Overview
Rivers Clothing is currently reviewing its projected earnings for the next six (6) months. As their IT Specialist, you saved this data in a List format. Now, they want to see the 2nd largest List element.
Method 1: Use sorted() and slicing
This method uses one of Python’s built-in string functions, sorted() to sort the List. Then slicing is applied to return the 2nd largest element.
projected = [25348.91, 28997.78, 32655.12, 36209.56, 40376.33, 44875.64] result = sorted(projected)[-2] print(result)
π‘Β Note: If duplicates are encountered, they will not be removed using this method. To do this, have a look at our detailed guide on how to remove duplicates in Python.
This code declares a List of Rivers Clothing’s projected earnings for the next six (6) months and saves them to projected.
Next, sorted() is passed one (1) argument, projected, and sorted (by default), in ascending order.
Output
If result was sent to the terminal without applying slicing (sorted(projected)) all List elements display.
[25348.91, 28997.78, 32655.12, 36209.56, 40376.33, 44875.64]To retrieve the 2nd largest element, apply slicing to the List (sorted(projected)[-2]).
40376.33π‘ Note: You could also pass the argument (reverse=True) to sort in descending order. Then, apply slicing to retrieve the 2nd largest element
(result = sorted(projected, reverse=True)[1]).
Method 2: Use List Comprehension and slicing
This method uses List Comprehension and max() to evaluate each List element and return the 2nd largest element.
projected = [888888888, 25348.91, 28997.78, 32655.12, 36209.56, 40376.33, 44875.64, 999999999] result = max([x for x in projected if x < max(projected)]) print(result) # 888888888
This code declares a List of Rivers Clothing’s projected earnings for the next six (6) months and saves them to projected.
Next, List Comprehension evaluates each element. If the current value is less than the largest element, it is appended to result
([x for x in projected if x < max(projected)]).
Output
If result was sent to the terminal without applying slicing (sorted(projected)) all elements display (except the maximum value: 44875.64).
[25348.91, 28997.78, 32655.12, 36209.56, 40376.33]To retrieve the 2nd largest element, apply slicing to the List (sorted().projected)[-1]
40376.33Method 3: Use set(), max() and slicing
This method is similar to Method 2. However, this method removes duplicates from an iterable (List) by applying set() before sorting. If duplicates are a concern, select this option.
projected = [25348.91, 28997.78, 32655.12, 36209.56, 40376.33, 44875.64] result = sorted(set(projected))[-2] print(result)
This code declares a List of Rivers Clothing’s projected earnings for the next six (6) months and saves them to projected.
Next, set() is called with one (1) argument, projected and removes duplicate values. Then, the List is sorted, (by default), in ascending order, and saved to result.
Output
If result was sent to the terminal without applying slicing (sorted(set(projected))) all unique elements display.
[25348.91, 28997.78, 32655.12, 36209.56, 40376.33, 44875.64]To retrieve the 2nd largest element, apply slicing to the List (sorted(set(projected))[-2]).
40376.33Method 4: Use np.partition()
This method calls in the numpy library and uses the np.partition() to automatically sort the List in ascending order and return the same.
- The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.
To install this library, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.
$ pip install numpy
Hit the <Enter> key on the keyboard to start the installation process.
If the installation was successful, a message displays in the terminal indicating the same.
Feel free to view the PyCharm installation guide for the required library.
import numpy as np projected = [25348.91, 28997.78, 32655.12, 36209.56, 40376.33, 44875.64] result = np.partition(projected, -3)[-2] print(result)
This code declares a List of Rivers Clothing’s projected earnings for the next six (6) months and saves them to projected.
Next, np.partition() is called and passed two (2) arguments: the iterable , and the element index to partition by (projected-2)
Output
If result was sent to the terminal without applying slicing (np.partition() all elements display.projected, -2)
[28997.78 25348.91 32655.12 36209.56 40376.33 44875.64] |
To retrieve the 2nd largest element, apply slicing to the List (np.partition().projected, -2)[-2]
40376.33 |
Method 5: Use heapq.nlargest()
This method calls on Python’s built-in heapq library to use nlargest() which sorts an iterable and returns the largest x number of elements.
import heapq projected = [25348.91, 28997.78, 32655.12, 36209.56, 40376.33, 44875.64] result = heapq.nlargest(2, projected)[-1] print(result)
This code declares a List of Rivers Clothing’s projected earnings for the next six (6) months and saves them to projected.
Next, heapq.nlargest() is called and passed two (2) arguments: the number of sorted elements to return, and an iterable, .projected
Output
If result was sent to the terminal without applying slicing (heapq.nlargest(2, ) the highest two (2) elements display.projected)
[44875.64, 40376.33] |
To retrieve the 2nd largest element, apply slicing to the List (heapq.nlargest(2, ).projected)[-1]
40376.33 |
Summary
There are more examples to accomplish this task. However, we selected five (5) of the most popular options for our article.