How to find second max value in list python without inbuilt function

Last update on May 28 2022 13:14:30 (UTC/GMT +8 hours)

Write a Python program to find the second largest number in a list.

Example - 1 :

How to find second max value in list python without inbuilt function

Example - 2 :

How to find second max value in list python without inbuilt function

Example - 3 :

How to find second max value in list python without inbuilt function

Example - 4 :

How to find second max value in list python without inbuilt function

Sample Solution:-

Python Code:

def second_largest(numbers): if (len(numbers)<2): return if ((len(numbers)==2) and (numbers[0] == numbers[1]) ): return dup_items = set() uniq_items = [] for x in numbers: if x not in dup_items: uniq_items.append(x) dup_items.add(x) uniq_items.sort() return uniq_items[-2] print(second_largest([1,2,3,4,4])) print(second_largest([1, 1, 1, 0, 0, 0, 2, -2, -2])) print(second_largest([2,2])) print(second_largest([1]))

Sample Output:

3 1 None None

Flowchart:

How to find second max value in list python without inbuilt function

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the second smallest number in a list.
Next: Write a Python program to get unique values from a list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



How to delete a file or folder?

  • os.remove() removes a file.
  • os.rmdir() removes an empty directory.
  • shutil.rmtree() deletes a directory and all its contents

Path objects from the Python 3.4+ pathlib module also expose these instance methods:

  • pathlib.Path.unlink() removes a file or symbolic link.
  • pathlib.Path.rmdir() removes an empty directory.

Ref: https://bit.ly/2zFlhuR

This article is created to cover some programs in Python, that find and prints second largest number or element in a given list. Here are the list of programs covered in this article:

  • Find Second Largest Number in a List of 10 elements using for Loop
  • Find Second Largest Number in a List of N elements using for Loop
  • Find Second Largest Number in a List of given Size using max() Method

Find Second Largest Number in List without using Function

The question is, write a Python program that find second largest element in a list using for loop. Here is its answer:

nums = [] print("Enter 10 Elements (Numbers) for List: ") for i in range(10): nums.append(int(input())) large = nums[0] for i in range(10): if large<nums[i]: large = nums[i] secondLarge = nums[0] for i in range(10): if secondLarge<nums[i]: if nums[i]!=large: secondLarge=nums[i] print("\nSecond Largest Number is: ") print(secondLarge)

Here is its sample run:

How to find second max value in list python without inbuilt function

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.