GFG App
Open App
Browser
Continue

# Python | Maximum and minimum element’s position in a list

Given a list of N integers, find the maximum and minimum element’s position in the Python list

Example:

```Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
The minimum is at position 3```

### Method 1: Using a native approach

The naive approach will be to traverse in the list and keep track of the minimum and maximum along with their indices. We have to do N comparisons for minimum and at the same time N comparisons for maximum. Below is the implementation of the naive approach.

## Python3

 `gfg_list ``=` `[``8``, ``1``, ``7``, ``10``, ``5``]`   `# min and max indexes are taken 1st element` `# In some cases list might be a single element` `min_ele, max_ele ``=` `gfg_list[``0``], gfg_list[``0``]`   `for` `i ``in` `range``(``1``, ``len``(gfg_list)):` `  `  `    ``if` `gfg_list[i] < min_ele:` `        ``min_ele ``=` `gfg_list[i]` `        `  `    ``if` `gfg_list[i] > max_ele:` `        ``max_ele ``=` `gfg_list[i]` `        `  `print``(``'Minimum Element in the list'``, gfg_list, ``'is'``, min_ele)`   `print``(``'Maximum Element in the list'``, gfg_list, ``'is'``, max_ele)`

Output:

```Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10```

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of given list.

### Method 2: Using the inbuilt function

Python’s inbuilt function allows us to find it in one line, we can find the minimum in the list using the min() function and then use the index() function to find out the index of that minimum element. Similarly we can do the same for finding out the maximum element using the max() function and then find out the index of the maximum element using the index() inbuilt function in python.

Note: index() returns the index of the first occurrence in case there are multiple occurrences of an element. So if maximum (or minimum) occurs more than once, the first occurrence is returned. Below is the implementation of the above approach:

## Python3

 `# function to find minimum and maximum position in list` `def` `minimum(a, n):`   `    ``# inbuilt function to find the position of minimum` `    ``minpos ``=` `a.index(``min``(a))`   `    ``# inbuilt function to find the position of maximum` `    ``maxpos ``=` `a.index(``max``(a))`   `    ``# printing the position` `    ``print` `"The maximum is at position"``, maxpos ``+` `1` `    ``print` `"The minimum is at position"``, minpos ``+` `1`     `# driver code` `a ``=` `[``3``, ``4``, ``1``, ``3``, ``4``, ``5``]` `minimum(a, ``len``(a))`

Output:

```The maximum is at position 6
The minimum is at position 3```

Time complexity of these functions is O(n), as they need to iterate over the entire list once.
Auxiliary Space: This function uses a constant amount of extra space to store the indices of the minimum and maximum values. Therefore, the space complexity of the minimum() function is O(1)

### Method 3: Using Pandas

In this method, we will use the idxmin() and idxmax() to print the max index and min index using the Pandas module.

## Python3

 `import` `pandas as pd`   `a ``=` `[``35``, ``41``, ``49``, ``37``, ``31``,` `     ``55``, ``23``, ``31``, ``18``, ``50``,` `     ``32``, ``37``, ``28``, ``27``, ``24``, ``35``]`   `print``(``"Min: "``, pd.Series(a).idxmin())` `print``(``"Max: "``, pd.Series(a).idxmax())`

Output:

```Min:  8
Max:  5```

Method 4: Using numpy argmax, argmin

## note: install numpy module using command “pip install numpy”

NumPy has argmax and argmin functions which can be used to find the indices of the maximum and minimum element. Here’s how to use these functions to find the maximum and minimum element’s position in a Python list:

Algorithm:

Convert the given list to  NumPy array using numpy.array().
Use argmax() and argmin() functions to find the index of maximum and minimum element in the array.
Print the index of the maximum and minimum element by adding 1 to the index.

## Python3

 `import` `numpy as np`   `def` `min_max_position(lst):` `    ``arr ``=` `np.array(lst)` `    ``min_pos ``=` `np.argmin(arr) ``+` `1` `    ``max_pos ``=` `np.argmax(arr) ``+` `1` `    ``print``(f``"The minimum is at position {min_pos}"``)` `    ``print``(f``"The maximum is at position {max_pos}"``)`   `# Example usage` `lst ``=` `[``3``, ``4``, ``1``, ``3``, ``4``, ``5``]` `min_max_position(lst)`

Output:

The minimum is at position 3
The maximum is at position 6

Time Complexity: O(n), where n is the length of the list. Converting the list to a  array takes O(n) time and finding the indices of the maximum and minimum elements using numpy.argmax() and numpy.argmin() functions also takes O(n) time. Overall time complexity of this method is O(n).

Auxiliary Space: O(n), since we are creating a NumPy array of size n to store the list elements.

My Personal Notes arrow_drop_up