for x in list that maximizes f(x)] <--newbie help

bearophileHUGS at bearophileHUGS at
Thu Dec 1 15:01:00 CET 2005

If the elements of mylist can be compared (es. not complex values),
then this can be a solution for you:

from math import sin as f
mylist = [float(2*i)/10 for i in xrange(10)]
pairs = [(f(x), x) for x in mylist]
ymax = max(pairs)[1]
print pairs, "\n"
print ymax

You can also try this, for Py2.4:
print max((f(x), x) for x in mylist)[1]


More information about the Python-list mailing list