Change language

# Python | Largest, smallest, second largest, second smallest on the list

| |

Because, unlike other programming languages, there are no arrays in Python, but a list. Working with lists is easier and more convenient than arrays. What’s more, Python’s extensive built-in functions make the task easier. So, using these methods, let’s try to find the different ranges of the number in the given list.
Examples:

`  Input:  list = [12, 45, 2, 41, 31, 10, 8, 6, 4]  Output:  Largest element is: 45 Smallest element is: 2 Second Largest element is: 41 Second Smallest element is: 4  Input:  list = [22, 85, 62, 40, 55, 12, 39, 2, 43]  Output:  Largest element is: 85 Smallest element is: 2 Second Largest element is: 62 Second Smallest element is: 12 `

The approach is simple. Python allows us to sort a list using the list () function. Using this, we can find different ranges of numbers in the list, from there, after sorting. Like the first position must contain the smallest and the last element must be the largest.

 ` # Python prog to illustrate the following in the list ` ` def ` ` find_len (list1): ` ` length ` ` = ` ` len ` ` ( list1) ` ` list1.sort () ` ` print ` ` (` ` "Largest element is: "` `, list1 [length ` ` - ` ` 1 ` `]) ` ` print ` < code class = "plain"> ( ` "Smallest element is:" ` `, list1 [` ` 0 ` `]) ` ` ` ` print ` ` (` ` "Second Largest element is:" ` `, list1 [length ` ` - ` ` 2 ` `]) ` ` print ` ` (` ` "Second Smallest element is: "` `, list1 [` ` 1 ` `]) `   ` Driver code ` ` list1 ` ` = ` ` [` ` 12 ` `, ` ` 45 , 2 , 41 , 31 , 10 , 8 , 6 , 4 ] `` Largest = find_len (list1) `

Exit:

` Largest element is: 45 Smallest element is: 2 Second Largest element is: 41 Second Smallest element is: 4 `

Below is another traditional method for performing the following calculations. The algorithm is simple: we take a number and compare it with all the other numbers in the list, and we get the largest, smallest, second largest, and second smallest element.

` `

` # Python program to find the largest, smallest, # second largest and second smallest in # list with complexity O (n) def Range (list1):  largest = list1 [ 0 ]  lowest = list1 [ 0 ]  largest2 = None lowest2 = None for item in list1 [ 1 :]:  if item" largest:  largest2 = largest largest = item  elif largest2 = = None or largest2 & lt; item:  largest2 = item  if item & lt; lowest:  lowest2 = lowest lowest = item  elif lowest2 = = None or lowest2" item:  lowest2 = item      print ( "Largest element is:" , largest)  print ( "Smallest element is:" , lowest)  print ( "Second Largest element is: " , largest2)    print ( "Second Smallest element is:" , lowest2)      Driver code list1 = [ 12 , 45 , 2 , 41 , 31 , 10 , 8 , 6 , 4 ] Range (list1) `

` ` Exit :

` Largest element is: 45 Smallest element is: 2 Second Largest element is: 41 Second Smallest elem ent is: 4 `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries