There are various data structures in Python that can store multiple items of multiple data types like string, float, integer, list, set, etc. at once. In a list, items can be changed, modified, deleted, and called at any time. All these operations can be performed on a list because all the elements in a list are stored with specific index values.

In this tutorial, we will see how to find the index of the maximum value in a list in Python.

Table of Contents

- Use for loop to find out the index of the maximum value in a list.
- Use the max() and list.index() functions to find out the index of the maximum value in a list.
- Use the enumerate() function to find out the index of the maximum value in a list.
- Use the numpy.argmax() function of the NumPy library to find out the index of the maximum value in a list.
- Find out the index of the maximum value that occurs more than once in a list

## Use `for loop`

to find out the index of the maximum value in a list.

A `for loop`

in Python executes a block of code a certain number of times until the loop has iterated over all the items in an iterable like list.

Example:

1 2 3 4 5 6 7 8 9 10 |
input_list = [15,20,35,42,12,8] max = input_list[0] index = 0 for i in range(1,len(input_list)): if input_list[i] > max: max = input_list[i] index = i print(f'Index of the maximum value is : {index}') |

Output:

- Here, first, we define the input list in which we have to find the index of the maximum value.
- Then we initialize the
`max`

function that is used to find the item with the highest value, or the item with the highest value of all the items in an iterable like list. - Then we initialize the index variable that will help us to find out the index of the item with the highest value in the given list.
- After that, we start with the
`for loop`

and we iterate over all the items in the list. - Finally, we print the required output.

## Use the `max()`

and `list.index()`

functions to find out the index of the maximum value in a list.

In this method, we will be using two python functions, i.e, the `max()`

function and the `list.index()`

function.

The `max()`

function in Python is used to find the item with the highest value, or the item with the highest value of all the items in an iterable like list.

The `list.index()`

is a built-in function in Python that is used to locate a specific element from the starting of a list and when the element is located, this function returns the lowest index. The index values of a list start from `0`

.

Example:

1 2 3 4 5 6 |
input_list = [15,20,35,42,12,8] max_value = max(input_list) index = input_list.index(max_value) print(index) |

Output:

- In the above code, the input list is stored in a variable at the starting.
- Then the
`max()`

function is used to find out the element with the highest value in the list. - After that, the
`index()`

function is used to find out the index of the highest value. - Finally, the required output is printed.

## Use the `enumerate()`

function to find out the index of the maximum value in a list.

The `enumerate()`

function in Python helps in looping over a specific iterable object and keeps check of the number of total iterations that have occurred in the whole process. This function is very useful whenever an array, a set, or a list of values is involved and there is a need of iterating through the entire array, set, or list.

Example:

1 2 3 4 5 |
input_list = [15,20,35,42,12,8] max_value = max(input_list) print([index for index, item in enumerate(input_list) if item == max_value]) |

Output:

- Note that here also the
`max()`

function is used to find out the element with the highest value in the list.

## Use the `numpy.argmax()`

function of the `NumPy`

library to find out the index of the maximum value in a list.

The `NumPy`

is one of the most commonly used libraries in Python. This library helps by providing various mathematical operations and functions to deal with large numerical data effectively. This library is used for dealing with the areas like arrays, matrices, linear algebra, and fourier transform.

The `numpy.argmax()`

function of the `NumPy`

library helps in returning the indices of the element of the highest value of an array or a list in a particular axis.

Example:

1 2 3 4 5 6 |
import numpy input_list = [15,20,35,42,12,8] max_value = numpy.argmax(input_list) print(max_value) |

Output:

- First, we start by importing the
`NumPy`

library. - Then we store the input list in a variable called
`input_list`

. - After that, we use the
`numpy.argmax()`

function on the input list and store the resultant in a variable. - Finally, we print the required output.

## Find out the index of the maximum value that occurs more than once in a list

For doing this, `list comprehension`

is used that will be used to store multiple index values inside a list.

In Python, `list comprehension`

is one of the easiest methods for creating new lists by using the elements present in an already created list. For example, one can create a list containing cars from a list containing all kinds of automobiles.

Example:

1 2 3 4 5 6 |
input_list = [15,20,35,42,12,8,19,42] max_value = max(input_list) index_value = [index for index in range(len(input_list)) if input_list[index] == max_value] print(index_value) |

Output:

That’s all about Find index of max value in list in python.