How to find the index of the max value in a list for Python?

Question:

I have a list, and I need to find the maximum element in the list and also record the index at which that max element is at.

This is my code.

list_c = [-14, 7, -9, 2]

max_val = 0
idx_max = 0

for i in range(len(list_c)):
    if list_c[i] > max_val:
        max_val = list_c[i]
        idx_max = list_c.index(i)

return list_c, max_val, idx_max

Please help. I am just beginning to code and got stuck here.

Asked By: Spark_101

||

Answers:

All you need is .max() and .index() function. First, you find max element of the list, then just simply get it index using index() function.

You can see a complete tutorial here: https://www.kite.com/python/answers/how-to-find-the-index-of-the-max-value-in-a-list-in-python

Answered By: CatProgrammer

.index can only be done on elements in the list. So, do idx_max = list_c.index(list_c[i])

max_val = list_c[i]
idx_max = list_c.index(list_c[i])

However, here is a shortened version

list_c = [-14, 7, -9, 2]
print(max(list_c),list_c.index(max(list_c)))
Answered By: user15801675
  • You are trying to find the index of i. It should be list_c[i].

Easy way/Better way is: idx_max = i. (Based on @Matthias comment above.)

  • Use print to print the results and not return. You use return inside functions.
  • Also your code doesn’t work if list_c has all negative values because you are setting max_val to 0. You get a 0 always.
list_c = [-14, 7, -9, 2]

max_val = 0
idx_max = 0

for i in range(len(list_c)):
    if list_c[i] > max_val:
        max_val = list_c[i]
        idx_max = i

print(list_c, max_val, idx_max)

[-14, 7, -9, 2] 7 1
Answered By: Ram

It is easiest to just apply logic to the entire list. You can get the max value of a list using max(list), and you can use the same index function you use above to get the index of this number in the list.

max_val = max(list_c)
idx_max = list_c.index(max_val)

If you want to loop over the values to find the max, then take note of the following:

  1. list.index(value) will find the index of that value in the list. In your case you are writing list.index(index_number) which doesnt make sense. ‘i’ already represents the list index, so just set idx_max = ‘i’.

  2. Further, you should note that if you set max_val to zero, if all the list items are less than zero, it will stay at zero during the loop and never find the max value. It is better to start by setting max_val to the first item of the list ie list_c[0].

     list_c = [-14, 7, -9, 2]
    
     max_val = list_c[0]
     idx_max = 0
    
     for i in range(len(list_c)):
         if list_c[i] > max_val:
             max_val = list_c[i]
             idx_max = i
    

NB ‘return’ is used to return values from a function. If you want to print the values, use print(list_c, max_val, idx_max).

Answered By: Daniel Redgate

You can use NumPy argmax() by converting your list to a NumPy array.

import numpy as np
list_c = [-14, 7, -9, 2]
print("Index max:",np.argmax(np.asarray(list_c)))
print("Value max:",list_c[np.argmax(np.asarray(list_c))])

Index max: 1

Value max: 7

Answered By: Claude COULOMBE

Looping twice through an array is inefficient. Use built-in enumerate() and max() with the comparison lambda expression returning the second element of the enumeration:

>>> list_c = [-14, 7, -9, 2]
>>> print(max(enumerate(list_c), key=lambda x: x[1]))
(1, 7)
Answered By: user1602
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.