Since, unlike other programming languages, Python does not have arrays, instead, it has list. Using lists is more easy and comfortable to work with in comparison to arrays. Moreover, the vast inbuilt functions of Python, make the task easier. So using these techniques, let’s try to find the various ranges of the number in a 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: 4Input :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 various ranges of numbers in a list, from there position, after being sorted. Like the first position must contain the smallest and the last element must be the greatest.

`# Python prog to illustrate the following in a list ` `def` `find_len(list1): ` ` ` `length ` `=` `len` `(list1) ` ` ` `list1.sort() ` ` ` `print` `(` `"Largest element is:"` `, list1[length` `-` `1` `]) ` ` ` `print` `(` `"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) ` |

*chevron_right*

*filter_none*

**Output:**

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

Below is another and traditional method to do the following calculation. The algorithm is simple, we take a number and compare it with all other numbers present in the list and get the largest, smallest, second largest and second smallest element.

`# Python program to find largest, smallest, ` `# second largest and second smallest in a ` `# 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 < item: ` ` ` `largest2 ` `=` `item ` ` ` `if` `item < 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) ` |

*chevron_right*

*filter_none*

**Output:**

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

## Recommended Posts:

- Python program to find second largest number in a list
- Python | Replace elements in second list with index of same element in first list
- Python - Filter the List of String whose index in second List contaons the given Substring
- Sort the values of first list using second list in Python
- Second largest value in a Python Dictionary
- Python | Sort a list according to the second element in sublist
- Python program to sort a list of tuples by second Item
- Python | Group by matching second tuple value in list of tuples
- Python - Find first element by second in tuple List
- Python - Group first elements by second elements in Tuple list
- Python - Sort by Frequency of second element in Tuple List
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python program to find smallest number in a list
- Python | Lexicographically smallest string in mixed list
- Python - Smallest integer possible from combination of list elements
- Python program to find N largest elements from a list
- Python program to find largest number in a list
- Python | Element with largest frequency in list

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.