Find Second Largest Element

Find the second largest distinct element in a list.

BeginnerTopic: List Programs
Back

Python Find Second Largest Element Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to find second largest distinct element in a list

numbers = list(map(float, input("Enter numbers separated by space: ").split()))

unique_numbers = sorted(set(numbers))

if len(unique_numbers) < 2:
    print("Need at least two distinct elements.")
else:
    print("Second largest element:", unique_numbers[-2])
Output
Enter numbers separated by space: 1 3 4 4 2
Second largest element: 3.0

Understanding Find Second Largest Element

We convert to a set to remove duplicates, sort, and take the second last element.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents