Zed-King Institute

    Second Largest Number in a List in Python

    mediumListsPython

    Once a list is sorted, its largest numbers sit at the end — and negative indexes count from the end.

    Quick answer

    Sort the list and take nums[-2]: after nums.sort() the largest number is at index -1 and the second largest at -2. If the list can repeat a number, remove duplicates first with sorted(set(nums))[-2]. Without sorting, keep two variables (first and second) and update them in one loop.

    The question

    Read a line of different integers separated by spaces and print the second largest number.

    The code

    program.py

    nums = list(map(int, input().split()))
    nums.sort()
    print("Second largest =", nums[-2])

    Input

    52 95 31 35 23 19 86

    Output

    Second largest = 86

    How it works

    1. 1Read the numbers into a list.
    2. 2nums.sort() arranges them from smallest to largest.
    3. 3nums[-1] is the largest, so nums[-2] is the second largest.

    What happens to the sample list

    Stepnums
    after input[52, 95, 31, 35, 23, 19, 86]
    nums.sort()[19, 23, 31, 35, 52, 86, 95]
    nums[-1]95 (largest)
    nums[-2]86 (second largest)

    Common mistakes

    • Using nums[1], which is the second smallest, not the second largest.
    • Writing nums = nums.sort() — sort() returns None, so the list is lost.

    Second largest number without sorting (one loop)

    first holds the largest value seen so far and second the one below it. When a bigger number arrives, the old first moves down to second. One pass, and the list is not changed.

    program.py

    nums = list(map(int, input().split()))
    first = second = None
    for x in nums:
        if first is None or x > first:
            second = first
            first = x
        elif x != first and (second is None or x > second):
            second = x
    print("Second largest =", second)

    Input

    12 45 7 33

    Output

    Second largest = 33

    When the list has duplicate numbers

    With 10 40 40 25, nums[-2] after sorting is 40 again. set() removes the repeats first, so the second largest distinct value is found.

    program.py

    nums = list(map(int, input().split()))
    print("Second largest =", sorted(set(nums))[-2])

    Input

    10 40 40 25

    Output

    Second largest = 25

    Using max() and remove()

    Take the largest out of the list, and the largest of what is left is the answer. Easy to explain in a viva.

    program.py

    nums = list(map(int, input().split()))
    nums.remove(max(nums))
    print("Second largest =", max(nums))

    Input

    8 3 15 11

    Output

    Second largest = 11

    Questions students ask

    How do you find the second largest number in a list in Python?
    Sort the list with nums.sort() and print nums[-2]. Negative indexes count from the end, so -1 is the largest and -2 the second largest. sorted(set(nums))[-2] does the same when numbers can repeat.
    What does nums[-2] mean in Python?
    The second item from the end of the list. nums[-1] is the last item and nums[-2] the one before it. On a sorted list those are the largest and the second largest values.
    Can I find the second largest number without sort()?
    Yes. Walk through the list once with two variables, first and second. When a number is bigger than first, the old first becomes second; when it is between them, it becomes the new second.

    Now solve a similar question yourself

    A new Python question every time, checked instantly.

    Practice questions

    Preparing for NIELIT O Level?

    This program is the kind asked in the M3 (Python) theory paper and the PR-3 practical exam.

    Want to build websites and apps with AI tools instead of writing every line yourself? See our AI Digital Pro course in Kaithal.

    More in Lists