Open In App

# Python | Largest, Smallest, Second Largest, Second Smallest in a List

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: 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

Approach#1: 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 their position, after being sorted. Like the first position must contain the smallest and the last element must be the greatest.

## Python3

 `# 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)`

Output:

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

Time Complexity: O(n*log(n))
Auxiliary Space: O(1)

Approach#2: Below is another 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.

## Python3

 `# 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 ``is` `None` `or` `largest2 < item:``            ``largest2 ``=` `item``        ``if` `item < lowest:``            ``lowest2 ``=` `lowest``            ``lowest ``=` `item``        ``elif` `lowest2 ``is` `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)`

Output

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

Time Complexity: O(N)
Auxiliary Space: O(1)

Approach#3: This task can be performed using max and pop methods of list. We can find largest and smallest element of list using max and min method after getting min and max element pop outs the elements from list and again use min and max element to get the second largest and second smallest element.

## Python3

 `# Python prog for finding largest, smallest``# Second largest and second smallest``def` `find_len( list1 ) :``    ``# max gives maximum element of list``    ``# Pop pull outs index element``    ``Lelmt ``=` `max``( list1 )``    ``list1.pop( list1.index( Lelmt ) )``    ``sLelmt ``=` `max``(list1)``    ``# Min gives minimum element of list``    ``# Pop pull outs index element``    ``Selmt ``=` `min``( list1 )``    ``list1.pop( list1.index( Selmt ) )``    ``sSelmt ``=` `min``( list1 )``    ``# Printing Min and max of list``    ``print``(``"Largest element is:"``, Lelmt )``    ``print``(``"Smallest element is:"``, Selmt )``    ``print``(``"Second Largest element is:"``, sLelmt )``    ``print``(``"Second Smallest element is:"``, sSelmt )` `# Driver Code``list1``=``[``12``, ``45``, ``2``, ``41``, ``31``, ``10``, ``8``, ``6``, ``4``]``Largest ``=` `find_len(list1)`

Output

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

The time complexity of this code is O(n) as it makes two linear scans of the list, one for finding the largest and smallest elements and another for finding the second largest and smallest elements. The pop operation is O(n) so the overall complexity is O(n).
The space complexity is O(1) as the code only uses a constant amount of extra space.

Approach #3 : Using sort() and extend() methods

## Python3

 `# Python prog to illustrate the following in a list``def` `find_len(list1):``    ``x``=``[]``    ``y``=``[]``    ``x.extend(list1)``    ``y.extend(list1)``    ``x.sort()``    ``y.sort(reverse``=``True``)``    ``print``(``"Largest element is:"``, y[``0``])``    ``print``(``"Smallest element is:"``, x[``0``])``    ``print``(``"Second Largest element is:"``, y[``1``])``    ``print``(``"Second Smallest element is:"``, x[``1``])` `# Driver Code``list1``=``[``12``, ``45``, ``2``, ``41``, ``31``, ``10``, ``8``, ``6``, ``4``]``Largest ``=` `find_len(list1)`

Output

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

Time Complexity : O(NlogN)
Auxiliary Space : O(N)

#### Approach#5: Using min and max

This approach finds the smallest, largest, second smallest, and second largest elements in a list by using the min and max functions and removing the elements from the list. The approach is concise and uses built-in functions for efficiency.

#### Algorithm

1. Use the min() and max() functions to find the smallest and largest elements.
2. Remove the smallest and largest elements from the list.
3. Use min() and max() functions again to find the second smallest and second largest elements.

## Python3

 `def` `find_elements(lst):``    ``smallest ``=` `min``(lst)``    ``largest ``=` `max``(lst)``    ``lst.remove(smallest)``    ``lst.remove(largest)``    ``second_smallest ``=` `min``(lst)``    ``second_largest ``=` `max``(lst)``    ``return` `smallest, largest, second_smallest, second_largest``lst``=``[``12``, ``45``, ``2``, ``41``, ``31``, ``10``, ``8``, ``6``, ``4``]``print``(find_elements(lst))`

Output

`(2, 45, 4, 41)`

Time complexity: O(n^2) due to the removal operation which takes O(n) time.
Auxiliary Space: O(1)