[Python-ideas] list.sort with a int or str key

Bruce Leban bruce at leapyear.org
Thu Sep 16 18:05:53 CEST 2010


-1

key='length' could reasonably mean
    lambda a:a.length
or
    lambda a:a['length']

an explicit lambda or itemgetter/attrgetter is clearer.

--- Bruce
http://www.vroospeak.com
http://j.mp/gruyere-security



On Thu, Sep 16, 2010 at 8:35 AM, Daniel Stutzbach <
daniel at stutzbachenterprises.com> wrote:

> list.sort, sorted, and similar methods currently have a "key" argument that
> accepts a callable.  Often, that leads to code looking like this:
>
> mylist.sort(key=lambda x: x[1])
> myotherlist.sort(key=lambda x: x.length)
>
> I would like to propose that the "key" parameter be generalized to accept
> str and int types, so the above code could be rewritten as follows:
>
> mylist.sort(key=1)
> myotherlist.sort(key='length')
>
> I find the latter to be much more readable.  As a bonus, performance for
> those cases would also improve.
> --
> Daniel Stutzbach <http://stutzbachenterprises.com>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100916/0ac89de3/attachment.html>


More information about the Python-ideas mailing list