strings and sort()

Paul Rubin phr-n2002a at nightsong.com
Thu Feb 21 00:18:05 EST 2002


Hans Nowak <wurmy at earthlink.net> writes:
> > > >>> def sort2(lst):
> > >       z = lst[:]
> > >       z.sort()
> > >       return z
> > > ...
> > Just write it like this:
> > 
> > def sort3(lst):
> >   lst.sort()
> >   return lst
> > 
> > The list comprehension already makes a temporary list.  You don't need
> > to copy it around again.
> 
> True... in this case. He may not always use it with a
> listcomp though, and then sort3 will sort the list in
> place and return it.

That's what sort2 is for ;-).  It's reasonable to provide both versions.
Actually, sort2 could then be written:

   def sort2(lst):
      return sort3(lst[:])



More information about the Python-list mailing list