Index of maximum element in list

bearophileHUGS at bearophileHUGS at
Sat Jan 26 17:28:12 CET 2008

Paul Rubin:
> def posmax(seq, key=lambda x:x):
>    return max(enumerate(seq), key=lambda k: key(k[1]))[0]

Is the Python max able to tell that's the identity function? I don't
think so, so your version may be slower and more memory hungry in the
common situation where you don't have a key function. So I think my
version is better :-)


More information about the Python-list mailing list