Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute

Stefan Arentz stefan.arentz at gmail.com
Sun Sep 9 05:19:42 EDT 2007


Miki <miki.tebeka at gmail.com> writes:

> >   steps.sort(key = lambda s: s.time)
> This is why attrgetter in the operator module was invented.
> from operator import attrgetter
> ...
> steps.sort(key=attrgettr("time"))

Personally I prefer the anonymous function over attrgettr :)

 S.



More information about the Python-list mailing list