Position of maximum and minimum element in a list - Python
Last Updated :
14 Apr, 2025
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)
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)
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)
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)
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)
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.
Similar Reads
Python | Positions of maximum element in list Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python - Maximum and Minimum K elements in Tuple Sometimes, while dealing with tuples, we can have problem in which we need to extract only extreme K elements, i.e maximum and minimum K elements in Tuple. This problem can have applications across domains such as web development and Data Science. Let's discuss certain ways in which this problem can
8 min read
Python - Maximum element in Cropped List Sometimes, while working with Python, we can have a problem in which we need to get maximum of list. But sometimes, we need to get this for between custom indices. This can be need of any domain be it normal programming or web development. Let's discuss certain ways in which this task can be perform
4 min read
Maximum and Minimum value from two lists - Python Finding the maximum and minimum values from two lists involves comparing all elements to determine the highest and lowest values. For example, given two lists [3, 5, 7, 2, 8] and [4, 9, 1, 6, 0], we first examine all numbers to identify the largest and smallest. In this case, 9 is the highest value
3 min read
Python Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row.Examples:Â Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Method 1: The idea is to run the loop for no_of_rows. Check each element inside the row and fi
5 min read
Python Program for Maximum and Minimum in a square matrix. Given a square matrix of order n*n, find the maximum and minimum from the matrix given. Examples: Input : arr[][] = {5, 4, 9, 2, 0, 6, 3, 1, 8}; Output : Maximum = 9, Minimum = 0 Input : arr[][] = {-5, 3, 2, 4}; Output : Maximum = 4, Minimum = -5 Naive Method : We find maximum and minimum of matrix
3 min read
Python - Find maximum length sub-list in a nested list In Python, we often work with nested lists (lists inside lists), and sometimes we need to find out which sub-list has the most items. In this article, we will explore Various methods to Find the maximum length of a sub-list in a nested list. Using max() Function with key=lenThe simplest and most eff
2 min read
Python - Add K to Minimum element in Column Tuple List Sometimes, while working with Tuple records, we can have a problem in which we need to perform task of adding certain element to max/ min element to each column of Tuple list. This kind of problem can have application in web development domain. Let's discuss a certain way in which this task can be p
8 min read
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
Python | Minimum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the minimum element of all the records received. This is a very common application that can occur in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 :
5 min read