Find Index of Max Item in Python List with Enumerate,Find Index of Max Item in Python List using index,Find Index of Max Item in Python List using numpy,Find Index of Max Item in Python List with a For Loop

Let’s see what this looks like in practice:

# Get Max Value from a List a_list = [10, 11, 14, 23, 9, 3, 35, 22] print(max(a_list)) # Returns 35

Let’s see how we can use the `.index()`

method to return the index of the max value:

# Get Index of Max Value from a List a_list = [10, 11, 14, 23, 9, 3, 35, 22] max_value = max(a_list) max_index = a_list.index(max_value) print(max_index) # Returns: 6 # You could also write: # max_index = a_list.index(max(a_list))

Let’s see how this works in Python:

# Get Indices of the Max Value from a List using a for loop a_list = [10, 11, 35, 14, 23, 9, 3, 35, 22] indices = [] max_value = max(a_list) for i in range(len(a_list)): if a_list[i] == max_value: indices.append(i) print(indices) # Returns: [2, 7]

One of these functions is the `argmax()`

function, which allows us to find the first instance of the largest value in the array.

# Get Index of the Max Value from a List using numpy import numpy as np a_list = [10, 11, 35, 14, 23, 9, 3, 35, 22] an_array = np.array(a_list) index = np.argmax(an_array) print(index) # Returns: 2

Avoid the solution with `itemgetter()`

presented in the other answers, and use instead

index_min = min(range(len(values)), key = values.__getitem__)

If you are dealing with numpy arrays or can afford `numpy`

as a dependency, consider also using

```
import numpy as np
index_min = np.argmin(values)
```

```
if is_min_level:
return values.index(min(values))
else:
return values.index(max(values))
```

You can find the min/max index and value at the same time if you enumerate the items in the list, but perform min/max on the original values of the list. Like so:

```
import operator
min_index, min_value = min(enumerate(values), key = operator.itemgetter(1))
max_index, max_value = max(enumerate(values), key = operator.itemgetter(1))
```

If you want to find the index of max within a list of numbers (which seems your case), then I suggest you use numpy:

```
import numpy as np
ind = np.argmax(mylist)
```

Possibly a simpler solution would be to turn the array of values into an array of value,index-pairs, and take the max/min of that. This would give the largest/smallest index that has the max/min (i.e. pairs are compared by first comparing the first element, and then comparing the second element if the first ones are the same). Note that it's not necessary to actually create the array, because min/max allow generators as input.

values = [3, 4, 5] (m, i) = max((v, i) for i, v in enumerate(values)) print(m, i) #(5, 2)

seq = [1.1412, 4.3453, 5.8709, 0.1314] seq.index(min(seq))

Last Updated : 16 Aug, 2022

**Example:**

```
Input: 3, 4, 1, 3, 4, 5
Output: The maximum is at position 6
The minimum is at position 3
```

**Output:**

```
Minimum Element in the list[8, 1, 7, 10, 5] is 1
Maximum Element in the list[8, 1, 7, 10, 5] is 10
```

STEP 3: Loop through the array from 0 to length of the array and compare the value of max with elements of the array.,In this program, we need to find out the largest element present in the array and display it. This can be accomplished by looping through the array from start to end by comparing max with all the elements of an array. If any of element is greater than max, then store a value of the element in max. Initially, max will hold the value of the first element. At the end of the loop, max represents the largest element in the array.,STEP 4: If any element is greater than max, max will hold the value of that element.,In the above array, initially, max will hold the value 25. In the 1st iteration, max will be compared with 11, since 11 is less than max. Max will retain its value. In the next iteration, it will be compared to 7, 7 is also less than max, no change will be made to the max. Now, max will be compared to 75. 75 is greater than max so that max will hold the value of 75. Continue this process until the end of the array is reached. At the end of the loop, max will hold the largest element in the array.

Largest element present in given array: 75