Lists in python are one of the most used data structures. In this article, we will discuss different ways to find the index of max value in a list in python.
- Find the Index of Max Value in a List Using for Loop in Python
- Find the Index of Max Value in a List Using the max() Function and index() Method
- Find Indices of Max Value in Case of Multiple Occurrences Using the max() Function and For Loop
- Find Indices of Max Value in Case of Multiple Occurrences Using the max() Function and List Comprehension
- Find Indices of Max Value in Case of Multiple Occurrences Using max() And enumerate() Function
- Find the Index of Max Value in a List in Python Using the Numpy Module
- Conclusion
Find the Index of Max Value in a List Using for Loop in Python
To find the index of max value in a list using for loop, we will use the following procedure.
- First, we will initialize a variable
max_index
to 0 assuming that the first element is the maximum element of the list. - After that, we will find the length of the list using the
len()
function. Thelen()
function takes a list as its input argument and returns the length of the list. - Once we get the length of the list, we will use the
range()
function and a for loop to iterate over the list. While iteration, at each index in the list, we will check if the element at the current index is greater than the element at the indexmax_index
. - If we find an index where the element is greater than the element at index
max_index
, we will assign the current index to the variablemax_index
.
After iteration of the entire list, we will get the index of the maximum element in the variable max_index
.
You can observe this in the following example.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 21, 12]
print("The list is:", myList)
max_index = 0
list_len = len(myList)
for index in range(list_len):
if myList[index] > myList[max_index]:
max_index = index
print("Index of the maximum element is:", max_index)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 21, 12]
Index of the maximum element is: 7
In the above case, we get the leftmost index of the maximum element if there are multiple occurrences of the maximum element.
To get the rightmost index of the maximum element, you can use the greater than or equal to (>=)
operator instead of the greater than (>)
operator while comparing the elements as shown below.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_index = 0
list_len = len(myList)
for index in range(list_len):
if myList[index] >= myList[max_index]:
max_index = index
print("Index of the maximum element is:", max_index)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Index of the maximum element is: 9
Find the Index of Max Value in a List Using the max() Function and index() Method
To write a more pythonic code, you can use the max()
function and the index()
method to find the index of the max value in a list in python.
The max() Function
The max()
function takes a container object like a list, tuple, or a set as its input argument. After execution, it returns the maximum element of the container object.
For example, if we give a list as an input argument to the max()
function, it will return the maximum element of the list. You can observe this in the following example.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_val = max(myList)
print("The maximum value is:", max_val)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
The maximum value is: 55
The index() Method
The index()
method, when invoked on a list, takes an element as its input argument and returns the index of the first occurrence of the element from the start of the list. You can observe this in the following example.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
index = myList.index(23)
print("Index of 23 is:", index)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Index of 23 is: 2
When the input element is not present in the list, the index()
method raises a ValueError
exception saying that the given element is not present in the list as shown below.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
index = myList.index(112)
print("Index of 112 is:", index)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Traceback (most recent call last):
File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 3, in <module>
index = myList.index(112)
ValueError: 112 is not in list
To find the index of max value in a list in python, we will first find the maximum element in the list using the max()
function. After that, we will invoke the index()
method on the list with the maximum element as its input argument. After executing the index()
method, we will get the index of the maximum element in the list. You can observe this in the following example.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_val = max(myList)
index = myList.index(max_val)
print("Index of maximum value is:", index)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Index of maximum value is: 7
Here, the maximum value 55 is present at two indices. However, the index()
method only returns the index of the leftmost occurrence of the element.
Find Indices of Max Value in Case of Multiple Occurrences Using the max() Function and For Loop
In the approach using the max()
function and index()
method, we can only find the first index of max value in a list in python. To find the index of the max value in a list in case of multiple occurrences of the max value, we can use the following approach.
- First, we will find the maximum value in the list using the
max()
function. - Then, we will create a list named
list_indices
to store the indices of the maximum value in the list. - After that, we will find the length of the input list using the
len()
function. Thelen()
function takes the list as its input argument and returns the length of the list. We will store the length in a variablelist_len
. - After obtaining the length of the list, we will create a sequence of numbers from 0 to
list_len
using therange()
function. Therange()
function takeslist_len
as its input argument and returns a sequence containing numbers from 0 tolist_len-1
. - Now, we will iterate over the sequence of numbers using a for loop. While iteration, we will check if the element in the list at the index equal to the current number in the sequence is equal to the maximum value or not.
- If we find an index in the sequence at which there is the maximum value present in the list. We will store the index in
list_indices
using theappend()
method. Theappend()
method, when invoked on the list, will take the index value as its input argument and will add it to the list.
After execution of the for loop, we will get all the indices of the maximum value in list_indices
. You can observe this in the following example.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_val = max(myList)
list_indices = []
list_len = len(myList)
sequence = range(list_len)
for index in sequence:
if myList[index] == max_val:
list_indices.append(index)
print("Indices of maximum element are:", list_indices)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Indices of maximum element are: [7, 9]
Find Indices of Max Value in Case of Multiple Occurrences Using the max() Function and List Comprehension
Instead of using the for loop, you can use list comprehension along with the range()
function and the max()
function to obtain a list of indices of max value from a given input list as shown below.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
max_val = max(myList)
list_len = len(myList)
sequence = range(list_len)
list_indices = [index for index in sequence if myList[index] == max_val]
print("Indices of maximum element are:", list_indices)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Indices of maximum element are: [7, 9]
Find Indices of Max Value in Case of Multiple Occurrences Using max() And enumerate() Function
The enumerate()
function is used to assign a counter to the elements of a container object. It takes a container object like a list and returns a list of tuples. In the list of tuples, each tuple contains a number denoting the element’s index as its first element and the corresponding value in the list as its second input argument. You can observe this in the following example.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
enumerated_list = list(enumerate(myList))
print("Enumerated list is:", enumerated_list)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Enumerated list is: [(0, 1), (1, 2), (2, 23), (3, 32), (4, 12), (5, 44), (6, 34), (7, 55), (8, 46), (9, 55), (10, 21), (11, 12)]
To find the indices of the max element using the enumerate()
function, we will use the following steps.
- First, we will find the maximum element in the input list using the
max()
function. - Then, we will create an empty list named
max_indices
to store the indices of the max element in the list. - After creating the list, we will obtain the enumerated list of tuples using the
enumerate()
function. - Once we get the enumerated list, we will iterate through the tuples using a for loop.
- While iteration, we will check if the current value in the tuple is equal to the maximum value. If yes, we will store the index associated with the element in the tuple in
max_indices
. For this, we will use invoke theappend()
method onmax_indices
with the current index as its input argument. - If the current value in the tuple is not equal to the maximum value, we will move to the next tuple.
After execution of the for loop, we will get all the indices of the max element in the max_indices
list. You can observe this in the following code.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
enumerated_list = list(enumerate(myList))
max_indices = []
max_element = max(myList)
for element_tuple in enumerated_list:
index = element_tuple[0]
element = element_tuple[1]
if element == max_element:
max_indices.append(index)
print("Indices of maximum element are:", max_indices)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Indices of maximum element are: [7, 9]
Instead of using the for loop in the above example, you can use list comprehension with the enumerate()
function to find the indices of the maximum element in the list as shown below.
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
enumerated_list = list(enumerate(myList))
max_element = max(myList)
max_indices = [index for (index, element) in enumerated_list if element == max_element]
print("Indices of maximum element are:", max_indices)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Indices of maximum element are: [7, 9]
Find the Index of Max Value in a List in Python Using the Numpy Module
We can also use the numpy module to find the index of max value in a list in python. The numpy module provides us with the argmax()
method to find the index of the max value in the list. The argmax()
method, when invoked on a numpy array, returns the index of the maximum element.
To find the max value in a list in python, we will first convert our input list into a numpy array using the array()
constructor. The array()
constructor takes a container object like a list as its input argument. After execution, it returns a numpy array containing the same number of elements as in the input container object.
After creating the numpy array from the input list, we will use the argmax()
function to find the index of the max value in the list as shown below.
import numpy
myList = [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
print("The list is:", myList)
array = numpy.array(myList)
max_index = array.argmax()
print("Index of maximum element is:", max_index)
Output:
The list is: [1, 2, 23, 32, 12, 44, 34, 55, 46, 55, 21, 12]
Index of maximum element is: 7
If there are multiple occurrences of the max value, the argmax()
method will return the index of the first occurrence of the max value from the left side of the list. You can observe this in the above example.
Conclusion
In this article, we have discussed different ways to find the index of max value in a list in python. If you need to find only the index of the first occurrence of the max value, you can use the approach with the max()
function and the index()
method. In case you need to obtain all the indices of the occurrence of the max value, you should use the approach using the max()
function and the enumerate()
function.
I hope you enjoyed reading this article. To learn more about python programming, you can read this article on how to remove all occurrences of a character in a list in Python. You might also like this article on how to check if a python string contains a number.
Stay tuned for more informative articles.
Happy Learning!
The post Find the Index of Max Value in a List in Python appeared first on PythonForBeginners.com.