By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,806 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,806 IT Pros & Developers. It's quick & easy.

sorted or .sort() ?

P: n/a
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.

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)?
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
Peter Bengtsson <pe*****@gmail.comwrites:
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
Jun 27 '08 #2

P: n/a
Peter Bengtsson <pe*****@gmail.comwrites:
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)?
Yes. Note that it's not "the listish object", the "sort" method is
implemented on actual lists, not on any sequence.
Jun 27 '08 #3

P: n/a
On Jun 16, 5:11*am, Peter Bengtsson <pete...@gmail.comwrote:
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.

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)?
Here's how sorted() works:

def sorted(iterable, *args, **kwds):
s = list(iterable)
s.sort(*args, **kwds)
return s

So, sorted() runs at the same speed as list.sort() except for the step
where the input gets copied. For list inputs, that extra time is
trivial compared to the cost of actually doing the sort. I wouldn't
worry about the negligible performance difference. Use whichever fits
best in your program.

Raymond
Jun 27 '08 #4

P: n/a
Ben Finney <bi****************@benfinney.id.auwrote:
Peter Bengtsson <pe*****@gmail.comwrites:
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.
Good advice with one caveat: sorted() was only introduced in python
2.4 so if your code must run on earlier versions then use list.sort()

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.