
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
Array is given,we have to find out maximum, minimum,secondlargest, second smallest number.
Algorithm
Step 1: input list element Step 2: we take a number and compare it with all other number present in the list. Step 3: get maximum, minimum, secondlargest, second smallest number.
Example code
# To find largest, smallest, second largest and second smallest in a List def maxmin(A): maxi = A[0] secondsmax = A[0] mini = A[0] secondmini = A[0] for item in A: if item > maxi: maxi = item elif secondsmax!=maxi and secondsmax < item: secondsmax = item elif item < mini: mini = item elif secondmini != mini and secondmini > item: secondmini = item print("Largest element is ::>", maxi) print("Second Largest element is ::>", secondsmax) print("Smallest element is ::>", mini) print("Second Smallest element is ::>", secondmini) # Driver Code A=list() n=int(input("Enter the size of the List ::")) print("Enter the number ::") for i in range(int(n)): k=int(input("")) A.append(int(k)) maxmin(A)
Output
Enter the size of the List ::6 Enter the number :: 12 30 2 34 90 67 Largest element is ::> 90 Second Largest element is ::> 67 Smallest element is ::> 2 Second Smallest element is ::> 12
- Related Articles
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- C program to find the second largest and smallest numbers in an array
- Python program to find the second largest number in a list
- C++ program to find Second Smallest Element in a Linked List
- Python Program to Find the Second Largest Number in a List Using Bubble Sort
- Program to find second largest digit in a string using Python
- C# Program to get the smallest and largest element from a list
- Program to find Smallest and Largest Word in a String in C++
- Find smallest and largest elements in singly linked list in C++
- Program to find k-sized list where difference between largest and smallest item is minimum in Python
- Find the Second Largest Element in a Linked List in C++
- Find the smallest and second smallest elements in an array in C++
- Python Program To Find the Smallest and Largest Elements in the Binary Search Tree
- Maximum sum of smallest and second smallest in an array in C++ Program

Advertisements