where or filter on list

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Wed Aug 30 10:39:27 EDT 2006


Duncan Booth:
> And for Python 2.5 users only we have the exciting new option of:
> >>> foo = [5, 2, -1, -7, 3, -6, 2, 12]
> >>> min(foo, key=abs)
> -1

Good. This is possibility for older Python:

l = [(rnd()-0.5) * 20 for i in xrange(1000)]
print min( (abs(el), el) for el in l )[1]

Bye,
bearophile




More information about the Python-list mailing list