469,955 Members | 2,080 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

sorted or .sort() ?

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
4 7151
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
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
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
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.

Similar topics

1 post views Thread by Kamilche | last post: by
15 posts views Thread by KraftDiner | last post: by
7 posts views Thread by ritchie | last post: by
99 posts views Thread by Shi Mu | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
4 posts views Thread by Gaijinco | last post: by
9 posts views Thread by Jae | last post: by
4 posts views Thread by Santosh Nayak | last post: by
reply views Thread by JosAH | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.