sorted or .sort() ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Bengtsson

    sorted or .sort() ?

    My poor understanding is that the difference between `sorted(somelis t,
    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)?
  • Ben Finney

    #2
    Re: sorted or .sort() ?

    Peter Bengtsson <peterbe@gmail. comwrites:
    My poor understanding is that the difference between `sorted(somelis t,
    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

    Comment

    • Hrvoje Niksic

      #3
      Re: sorted or .sort() ?

      Peter Bengtsson <peterbe@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.

      Comment

      • Raymond Hettinger

        #4
        Re: sorted or .sort() ?

        On Jun 16, 5:11 am, Peter Bengtsson <pete...@gmail. comwrote:
        My poor understanding is that the difference between `sorted(somelis t,
        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

        Comment

        • Nick Craig-Wood

          #5
          Re: sorted or .sort() ?

          Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
          Peter Bengtsson <peterbe@gmail. comwrites:
          >
          My poor understanding is that the difference between `sorted(somelis t,
          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 <nick@craig-wood.com-- http://www.craig-wood.com/nick

          Comment

          Working...