3

Possible Duplicate:
Pythonic way to find maximum value and its index in a list?

Say, in list [2, 3, 6, 9, 2, 3, 1, 5, 7], I want to get 3 (position of item 9) as output.

A similar question but for numpy array

My intuition is to build a tuple, and sort the tuple, and get the biggest item's position. I believe there are many better ways....

Community
  • 1
  • 1
Flake
  • 4,377
  • 6
  • 30
  • 29

2 Answers2

4
pos = mylist.index(max(mylist))

This includes all internal python logic - Therefore the best possible implementation.

Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197
2

Like this:

lst = [2, 3, 6, 9, 2, 3, 1, 5, 7]
maxval = max(lst)
maxpos = lst.index(maxval)
Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88