Beginner Python Exercises

Find the Second Largest Value

Removing duplicates before sorting makes the desired rank unambiguous.

What is Find the Second Largest Value?

Removing duplicates before sorting makes the desired rank unambiguous.

Find the second distinct largest list value.

When should you use it?

  • Learn Python syntax through a practical example.
  • Build a foundation for larger programming problems.
  • Test an idea quickly without a local setup.

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]}")

Expected output

second=12

How it works

A set keeps distinct values and sorted places the largest values at the end.

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 Python IDE →