Index of max element in list Python
In this post, you will learn how to find the index of the max element in a list using the Python programming language.
A list is an arranged collection of elements. It can contain a list of various types of data objects with a comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements in a list. Finding the index of the maximum element in a list returns the position of the max value within the list. Here, we have mentioned different ways to find the index of the max element.
Get index of max element in a list using for loop in Python
Here, we have taken a list named 'listEles', which contains a list of integers. Initially, we assigned the first element of the list and stored it in the max variable. Next, we iterate the list using a loop from index 1 to the last element of the list. Within the loop, we compared the ith element, i.e., the current element of 'listEles' with the 'max' variable. If the value of the current element is greater than the value of the 'max', then we assign the value of the current element to the 'max' and the current index to the 'i'. At the end of the loop, we get the index of the maximum value from the list.
listEles = [45,22,55,23,63,12,59,14]
max = listEles[0]
index = 0
for i in range(1,len(listEles)):
if listEles[i] > max:
max = listEles[i]
index = i
print(f'Maximum index value : {index}')
Output of the above code:
Maximum index value : 4
Get index of max element in a list using the max() and list.index() in Python
In this post, we have used two Python functions, i.e., the max() function and the list.index() functions. The max() function of the Python returns the largest item in an iterable. The index() method returns the index of the specified element in the list.
my_list = [23,12,44,63,27,85,97]
max_value = max(my_list)
max_index = my_list.index(max_value)
print("Maximum index value : ",max_index)
Output of the above code:
Maximum index value : 6
Get index of max element in a list using enumerate() function in Python
The enumerate() is a pre-defined function in Python and is used to add a counter to an iteration and return it in the form of an enumerated object. The returned object is an enumerated object.
x_list = [71,23,53,26,89,91,12]
max_item = max(x_list)
print("Maximum index value :")
print([index for index, item in enumerate(x_list) if item == max_item])
Output of the above code:
Maximum index value :
[5]
Get index of max element in a list using numpy library in Python
NumPy is the fundamental package for scientific computing with Python. It is a highly optimised library for numerical operations. The support of NumPy makes the task easier. The numpy.argmax() function of the NumPy library helps in returning the indices of the max element of the array along a particular axis.
import numpy as np
x_list = [14,31,92,75,60,52]
n = np.array(x_list)
print(f'Maximum index value : {np.argmax(n)}')
Output of the above code:
Maximum index value : 2
Related Articles
Python Convert XML to CSV
Python Pandas Dataframe to CSV
Python Converting a CSV File to a MySQL Table
Convert Excel to CSV Python Pandas
Generate and download a CSV file in Django
Python write to csv file
Python Pandas Dataframe to CSV
Python Pandas CSV to Dataframe
Prettytable in Python
Python dict inside list
Convert dictionary to dataframe Python
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module