 # [python] index of absolute maximum value in a list

I know how to get the index of the maximum value in a list.

``````mylist = [-10,0,1]
idx_max_value = max(xrange(len(mylist)), key=mylist.__getitem__)
#>>> 2
``````

However, I need it to take the abs(-10) and so the result will be 0.

How can I do that?

I found this, but there they pass `abs` as argument to the `max()` so I cannot use it this way. Is there some trick that I can pass more than one arguments as `max(key=)`?

Also they get the value, and I need the index.

Hi, try this:

``max(enumerate(mylist), key = lambda x: abs(x))``
1 Like

Awesome @Dancergraham, thanks.

Could you please elaborate what you do there with the lambda?

That’s a anonymous function but I don’t get it completely.

Yes I am getting the max of an enumerated list : a list of tuples of (index, value)
The key is a lambda function which returns abs(item ) from the tuple. The max function therefore returns the tuple with the highest absolute value inside. I then stick a  at the very end of the line to get just the index from the returned tuple
Does that make sense? You may need to test these steps one by one to follow…

1 Like

Thanks, I think I got it now. 