Open In App

Position of maximum and minimum element in a list – Python

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, lists are one of the most common data structures we use to store multiple items. Sometimes we need to find the position or index of the maximum and minimum values in the list.

For example, consider the list li = [3, 5, 7, 2, 8, 1].

  • The maximum element is 8, and its index is 4.
  • The minimum element is 1, and its index is 5.

There are several ways to find the minimum and maximum element’s index in Python, let’s explore them one by one:

Using enumerate()

enumerate() function helps us get both the index and the value from a list in one go. This method makes the code cleaner and more efficient without manually keeping track of the index.

Python
a = [3, 5, 7, 2, 8, 1]

mxv = mnv = a[0]
mxp = mnp = 0

for i, value in enumerate(a):
    if value > mxv:
        mxv = value
        mxp = i
    if value < mnv:
        mnv = value
        mnp = i

print(mxp)
print(mnp)

Output
4
5

Explanation: enumerate() makes the code cleaner by giving both the index (i) and the value in each iteration. After that, logic remains the same as the loop method.

Using Loop

If we want to avoid using built-in functions and make the code more manual, we can use a loop to find the maximum and minimum elements and their positions. This method requires a bit more work but is still easy to understand.

Python
a = [3, 5, 7, 2, 8, 1]

mxv = mnv = a[0]
mxp = mnp = 0

for i in range(len(a)):
    if a[i] > mxv:
        mxv = a[i]
        mxp = i
    if a[i] < mnv:
        mnv = a[i]
        mnp = i

print(mxp)
print(mnp)

Output
4
5

Explanation: This approach manually tracks the maximum and minimum values while looping through the list. It updates their positions when a new max or min is found.

Using index() Function

The easiest way to find the position of the maximum and minimum elements in a list is by using Python’s built-in max() and min() functions along with index():

Python
a = [3, 5, 7, 2, 8, 1]

mxp = a.index(max(a))
mnp = a.index(min(a))

print(mxp)
print(mnp)

Output
4
5

Explanation:

  • max(a) returns the largest element.
  • a.index(max(a)) gives the index of that maximum value.
  • min(a) and a.index(min(a)) give the position of the smallest value.

Using NumPy argmax, argmin

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.

Python
import numpy as np

def min_max_pos(a):
    ar = np.array(a)
    mxp = np.argmax(ar)
    mnp = np.argmin(ar)
    
    print(mxp)
    print(mnp)

a = [3, 5, 7, 2, 8, 1]
min_max_pos(a)

Output
4
5

Explanation:

  • np.argmax() returns the index of the max value in the NumPy array.
  • np.argmin() returns the index of the min value.

Note: NumPy is faster for large datasets, making this a great option for numerical computing.

Using sorted()

sorted() method involves sorting the list and finding the positions of the maximum and minimum values based on their sorted positions. It’s not the most efficient, but it can be useful in some cases.

Python
a = [3, 5, 7, 2, 8, 1]

s = sorted(a)
mxp = a.index(s[-1]) 
mnp = a.index(s[0]) 

print(mxp)
print(mnp)

Output
4
5

Explanation: This method sorts the list and then finds the position of the first (min) and last (max) values in the original list. It’s not optimal for performance but useful when sorting is already needed.



Next Article

Similar Reads