sorted or .sort() ?

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Jun 16 14:50:22 CEST 2008


Peter Bengtsson <peterbe at gmail.com> writes:

> My poor understanding is that the difference between `sorted(somelist,
> key=lambda x:...)` and `somelist.sort(lambda x,y...)` is that one
> returns a new list and the other sorts in-place.

Yes.

> Does that mean that .sort() is more efficient and should be favored
> when you can (i.e. when you don't mind changing the listish object)?

No, it means you should choose the version that expresses what you
actually want to do.

Efficiency of the programmers — including the unknown number of
programmers who will have to read the code after you write it — is in
many cases a much more important criterion than efficiency of the CPU.
People's time continues to be much more expensive than computer time,
after all.

-- 
 \           "Are you pondering what I'm pondering?" "Umm, I think so, |
  `\        Brain, but what if the chicken won't wear the nylons?"  -- |
_o__)                                            _Pinky and The Brain_ |
Ben Finney



More information about the Python-list mailing list