Looking for a different version of sort

Jeff Epler jepler at unpythonic.net
Wed Jun 16 08:50:07 EDT 2004


> "Delaney, Timothy C (Timothy)" <tdelaney at avaya.com> wrote in message news:<mailman.31.1087353172.21521.python-list at python.org>...
> ...
> > There is no function available which sorts in-place and returns a
> > reference to the list sorted, but it's very simple to write a function
> > to do it ...
> > 
> > def inplace sort (l):
> >     l.sort()
> >     return l

On Wed, Jun 16, 2004 at 01:10:16AM -0700, Dan Bishop wrote:
> Or, alternatively:
> 
> sort = lambda l: (l.sort(), l)[1]

I'm not sure why you suggest this.  It benchmarks a little slower than
Timothy's version.  The speed difference probably results from
constructing and then discarding the tuple.

Jeff

$ cat /tmp/danb.py
def inplace_sort1(l):
    l.sort()
    return l

inplace_sort2 = lambda l: (l.sort(), l)[1]
$ timeit -s \ 
    'import sys; sys.path.append("/tmp"); from danb import inplace_sort2; l = []' \
    'inplace_sort2(l)'
1000000 loops, best of 3: 1.39 usec per loop
$ timeit -s \
    'import sys; sys.path.append("/tmp"); from danb import inplace_sort1; l = []' \
    'inplace_sort1(l)'
1000000 loops, best of 3: 1.1 usec per loop
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040616/14033134/attachment.sig>


More information about the Python-list mailing list