Python | Positions of maximum element in list
Last Updated :
16 May, 2023
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 achieve this task in this case.
Method 1: Using max() + enumerate() + list comprehension In this method, the combination of above functions is used to perform this particular task. This is performed in two steps. In 1st, we acquire the maximum element and then access the list using list comprehension and corresponding element using enumerate and extract every element position equal to maximum element processed in step 1.
Python3
# Python3 code to demonstrate working of
# Positions of maximum element in list
# Using list comprehension + max() + enumerate()
# initializing list
test_list = [8, 4, 6, 8, 2, 8]
# printing list
print("The original list : " + str(test_list))
# Positions of maximum element in list
# Using list comprehension + max() + enumerate()
temp = max(test_list)
res = [i for i, j in enumerate(test_list) if j == temp]
# Printing result
print("The Positions of maximum element : " + str(res))
OutputThe original list : [8, 4, 6, 8, 2, 8]
The Positions of maximum element : [0, 3, 5]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. max() + enumerate() + list comprehension performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method 2: Using numpy
Note: Install numpy module using command "pip install numpy"
The numpy library in python provides a function called "argwhere()" which can be used to find the indices of all occurrences of a specific value in an array.
Python3
import numpy as np
# initializing list
test_list = [8, 4, 6, 8, 2, 8]
# printing list
print("The original list : " + str(test_list))
res = np.argwhere(np.array(test_list) == max(test_list))
# Printing result
print("The Positions of maximum element : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
Output:
The original list : [8, 4, 6, 8, 2, 8]
The Positions of maximum element : [[0] [3] [5]]
Method 3: Using pandas
In this method we creates a series from the list, filters it using boolean indexing, and extracts the positions of the maximum value. The resulting positions are stored in a list and then printed.
Python3
import pandas as pd
# initializing list
test_list = [8, 4, 6, 8, 2, 8]
# printing list
print("The original list : " + str(test_list))
# create a pandas series from the original list
series = pd.Series(test_list)
# find the maximum value in the series
max_val = series.max()
# find the positions of maximum element in series
res = series[series == max_val].index.tolist()
# Printing result
print("The Positions of maximum element : " + str(res))
Output:
The original list : [8, 4, 6, 8, 2, 8]
The Positions of maximum element : [0, 3, 5]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), where n is the length of the input list.
Similar Reads
Position of maximum and minimum element in a list - Python 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 e
3 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 - 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
Python - K Maximum elements with Index in List GIven a List, extract K Maximum elements with their indices. Input : test_list = [5, 3, 1, 4, 7, 8, 2], K = 2 Output : [(4, 7), (5, 8)] Explanation : 8 is maximum on index 5, 7 on 4th. Input : test_list = [5, 3, 1, 4, 7, 10, 2], K = 1 Output : [(5, 10)] Explanation : 10 is maximum on index 5. Method
4 min read
Python - Ranged Maximum Element in String List Sometimes, while working with Python data, we can have a problem in which we have data in form of String List and we require to find the maximum element in that data, but that also in a certain range of indices. This is quite peculiar problem but can have application in data domains. Let's discuss c
4 min read