Beginner Python Exercises

Find the Second Largest Value in a Python List

Remove duplicates, sort, take the second from the end, or track the top two values in one pass.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Removing duplicates with set()
  • Sorting and indexing from the end
  • Handling lists with fewer than two distinct values
sorted(set([7, 23, 12, 23, 9]))
7[0]
9[1]
12[-2]
23[-1]
second = 12

Distinct values first

The problem

Finding the runner-up is common: the second highest score, the second cheapest flight. Sorting and taking the second-to-last item looks easy, but repeated values such as two scores of 23 give the wrong answer.

Think of it like this

Finding the second largest value is like awarding medals when two people tie for first. If you simply take the second name on the list, you might give silver to someone who also tied for gold. Merging the tied scores first makes the ranks clear.

From awarding medals to code
values🏃 All results, with a tie at 23
set(values)🤝 Merge the tied scores
sorted(...)📶 Line up the distinct scores
distinct[-2]🥈 The silver medal: 12
Remove duplicates first, then pick the rank.
In one sentence

The second largest value is the largest value that is smaller than the maximum, so repeated copies of the maximum do not count.

Key points

  1. set(values) removes the duplicate 23.
  2. sorted(...) puts the distinct values in ascending order: [7, 9, 12, 23].
  3. distinct[-2] is the second item from the end, 12.
  4. Without the set, sorted(values)[-2] would wrongly give 23.

When should you use it?

  • Rankings: second best score, runner-up prices.
  • Practicing sets to remove duplicates before choosing.
  • Interview questions about edge cases.

When not to use it

  • Lists with fewer than two distinct values: distinct[-2] raises IndexError, so check the length first.
  • Very large lists: one loop that keeps the two largest values avoids sorting.

Example code

Run code →
main.py
values = [7, 23, 12, 23, 9]
# Remove duplicates before selecting the second-highest rank.
distinct = sorted(set(values))
print(f"second={distinct[-2]}")
Get a Run button for this example

Result of running the code

second=12

Code explanation

set(values) keeps each value once, so the duplicate 23 is removed. sorted() turns the set into an ascending list, [7, 9, 12, 23], with the largest value last. distinct[-2] therefore picks the value just before the maximum, which is 12.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Ignoring duplicates

A repeated maximum is returned again as the second largest.

Problem
sorted(values)[-2]  # 23
Fix
sorted(set(values))[-2]  # 12

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges