Position of maximum and minimum element in a list - Python

Last Updated : 28 Sep, 2026

In Python, we can find the positions of maximum and minimum elements in a list using built-in functions, loops or NumPy. The position is represented by an index, which starts from 0.

Example: Consider the following list:

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

The maximum element is 8 at index 4, while the minimum element is 1 at index 5.

Using max(), min() and index()

max() and min() functions return the largest and smallest elements, respectively. We can use the index() method to find their positions in the list.

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

max_pos = a.index(max(a))
min_pos = a.index(min(a))

print(max_pos)
print(min_pos)

Output
4
5

Explanation:

  • max(a) returns the largest element, while min(a) returns the smallest.
  • index() finds the position of each element in the original list.
  • Both functions return the first occurrence when duplicate extreme values exist.

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:

  • max_val and min_val are initialized with the first element, and both positions start at 0.
  • enumerate() iterates through the list, providing each element's index and value.
  • Whenever a larger or smaller element is encountered, its corresponding value and position are updated.

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 NumPy argmax, argmin

NumPy has argmax and argmin functions which can be used to find the indices of the maximum and minimum element.

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.array() converts the list into a NumPy array.
  • np.argmax() returns the index of the maximum element.
  • np.argmin() returns the index of the minimum element.

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:

  • sorted() method sorts the list.
  • Then finds the position of the first (min) and last (max) values.

Handling an Empty List

It has no maximum or minimum element. Calling max() or min() without a default value on an empty list raises a ValueError. We can check whether the list is empty before finding the positions.

Python
a = []

if a:
    max_pos = a.index(max(a))
    min_pos = a.index(min(a))
    print(max_pos)
    print(min_pos)
else:
    print("List is empty")

Output
List is empty

Explanation:

  • The if condition checks whether the list contains elements.
  • If the list is non-empty, the maximum and minimum positions are calculated.
  • Otherwise, a message is displayed instead of attempting to access nonexistent elements.
Comment