A small suggestion for Python

Alex Martelli aleaxit at yahoo.com
Sun Jan 14 05:20:54 EST 2001


"Matt Dunford" <kno at jtan.com> wrote in message
news:G7595o.HKE.0.sinope at news.jtan.com...
> "Alex Martelli" <aleaxit at yahoo.com> writes:
>
> >"Matt Dunford" <kno at jtan.com> wrote in message
> >news:G74HuE.AGn.0.sinope at news.jtan.com...
> >    [snip]
> >> Although, I wouldn't mind having a builtin method that returns a sorted
> >> array.  Then we could do something like this:
> >>
> >> dict = { 'this' : 1, 'that' : 2 }
> >> for key in dict.keys().sort():
> >> print key
>
> >And instead we have to do
>
> >    for key in return_sorted(dict.keys()):
> >        print key
>
> >Is it that much of a problem...?  return_sorted's pretty trivial too:
>
> >def return_sorted(alist):
> >    alist.sort()
> >    return alist
>
> I have no problem with it.  But just to be pendantic, your return_sorted
def
> doesn't do what I'm trying to get at.  I'm thinking more along the lines
of
>
> def return_sorted(alist):
>     import copy
>     sorted = copy.copy(alist)
>     sorted.sort()
>     return sorted

I would call this return_sorted_copy.  It's not needed, often, and in
particular
it is not for the idiom you mentioned -- why make a totally needless copy of
the temporary list that .keys returns?  I'm anything but a speed freak, but
I
don't normally copy data just for the fun of it:-).


Alex






More information about the Python-list mailing list