Python – Position of maximum element in list


Python language is comprise various data structures and from it, the list is the most commonly used one. The elements are assigned inside the square brackets and separated by commas. The list can be manipulated to get the different methods and getting the position of the maximum element is one common method. The elements that are once defined inside the list data structure that is within the square brackets cannot be changed. In this article, the user will understand how to obtain the position of maximum element in list.

Approach

  • Approach 1 − Using the pandas library

  • Approach 2 − Using the max and index function

  • Approach 3 − Using the lambda function

Approach 1:  By using the pandas module in Python Program

By importing the ‘pandas’ module, the data frame function can be utilized. It is defined with one argument as the input. The major functions used for getting the required elements are the max function and its position of it using the index method.

Algorithm

  • Step 1 - The list is initialized with eight elements as positive and negative values

  • Step 2 - To utilize the data frame function, the necessary module must be imported.

  • Step 3 - The contents of max_val will then be printed by an updated list.

Example

#importing the essential library that is pandas
import pandas as pd
#Creating a list of elements
value1 = [1, 5, 9, -8, 9, 7, 6, 9]
#print function will print the input
print("value1: ", value1)
#data frame method defined with one argument as input
val = pd.DataFrame(value1)
max_val = val[val[0] ==val[0].max()].index.tolist()
#position of the element is returned
print("Positions of the maximum elements are: ", *max_val)

Output

value1: [1, 5, 9, -8, 9, 7, 6, 9]
Positions of the maximum elements are: 2 4 7

Approach 2:  By using the max() and index() function in Python Program

To find the positions of the maximum element, two functions are majorly used. The first one is for getting the maximum value and the other is to get the position of it using the index method. We have initialized the list data structure with a set of integer elements and the maximum element in the list is identified using the max function.

Algorithm

  • Step 1 - The list is initialized with five elements as positive and negative values

  • Step 2 -The new variable is created as max_val to store the result got from the max function.

  • Step 3 - Similarly, the position of it is found using the index function and result is stored in the position variable.

  • Step 3 - Then the position of the maximum element in the list is printed.

Example

#Creating the list of elements
list1 = [6, 10, 2, 30, 15]
#using the max() function to get the maximum element and store it in max_element
max_val = max(list1)
#finding the position using the index value
position = list1.index(max_val)
#print function will return the maximum element and its position
print("Position of maximum element is :", position)

Output

Position of maximum element is : 3

Approach 3:  By using the lambda function in Python Program

The function needs to be defined while using it, but in the case of the lambda function, it is not necessary to define the function as it is an anonymous function with key parameters.

Algorithm

  • The list is initialized with eight elements of both positive and negative elements.

  • For clarification, the input is printed as lists.

  • The lambda function always comes with the filter and len function

  • The filter function takes two arguments as key parameters and range.

  • According to the range, the len function will iterate through the list.

Example

# create a new list with integer elements
list1  = [3, 5, 80, -9, 80, 70, 60, 80]
#print function will print the input
print("list: ", list1)
max_val = list(filter(lambda a:list1[a] == max(list1), range(len(list1))))
#position of the maximum element
print("Positions of the maximum element is : ", max_val)

Output

list:  [3, 5, 80, -9, 80, 70, 60, 80]
Positions of the maximum element is :  [2, 4, 7]

Conclusion

The three approaches are demonstrated in this article. The approaches help the programmer print all the elements in the list with maximum value. Data handling is the most prominent one in this current technology and printing the position of maximum element is one of the fundamental functions.

Updated on: 01-Sep-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements