Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86853

Re: Sort list of dictionaries

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.030
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'function,': 0.09; 'modulo': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '(usually,': 0.16; 'charles': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function),': 0.16; 'lambda': 0.16; 'lambda:': 0.16; 'personally,': 0.16; 'subject:Sort': 0.16; 'subject:dictionaries': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'simpler': 0.24; 'fine': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '[1]': 0.29; 'am,': 0.29; 'statement': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'that.': 0.31; 'usually': 0.31; 'created': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'edge': 0.36; 'clear': 0.37; 'being': 0.38; 'thank': 0.38; 'fact': 0.38; 'extremely': 0.39; 'expression': 0.60; 'break': 0.61; 'new': 0.61; 'simple': 0.61; 'you.': 0.62; 'name': 0.63; 'skip:n 10': 0.64; 'between': 0.67; 'mar': 0.68; 'apart': 0.72; '"".': 0.84; '2015': 0.84; 'difference.': 0.84; 'peter,': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=v/p6QF/h4WrkXZlzmQbLyD/bwv3PzRts5helTKvr38s=; b=0jNlCCU9PVJAJcWDNZdEPTR/ji9jpSBTf1qSQD9Lig0f47Ha9LD9pmnZJ6f/Z4OiUr K2f+2Wh36mxpJmlBXz81KW5/wUtMEkpdC9bNvg/G0oOAZGIBuZGHC29qaDoNz1Kkc4gv Bz+rnfZf1OjKqkUEMCHKgYwStYk22nIZZS/rPQFImwoLCOVWNgFdL6BOsrIPB+jcHaSb NEzm8/4s3av0G7kIJFotH/aUcSEsw4FL2lTGqA5Dw+C1Sws6glcp4kg+ndU5N15RRTcm AyO5wBaPUfHn6/kvm6NweWpsi+eYru7DXntZnn6nynR967fR0i2C2TFykMLE2c4VX7jU 3OUQ==
MIME-Version 1.0
X-Received by 10.50.131.196 with SMTP id oo4mr3083030igb.2.1425398941887; Tue, 03 Mar 2015 08:09:01 -0800 (PST)
In-Reply-To <432a0d61-a8ba-46ca-bc53-318e234fe168@googlegroups.com>
References <f1a339d0-0386-43ba-b6b4-1aee39d75581@googlegroups.com> <mailman.56.1425321135.13471.python-list@python.org> <946797be-10e6-433b-9411-2db0d5697ac8@googlegroups.com> <8ccd65d8-3f1f-41ac-9092-9b0832d2fc49@googlegroups.com> <mailman.61.1425324201.13471.python-list@python.org> <432a0d61-a8ba-46ca-bc53-318e234fe168@googlegroups.com>
Date Wed, 4 Mar 2015 03:09:01 +1100
Subject Re: Sort list of dictionaries
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.22.1425398950.21433.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1425398950 news.xs4all.nl 2931 [2001:888:2000:d::a6]:44394
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86853

Show key headers only | View raw


On Wed, Mar 4, 2015 at 2:56 AM, Charles Heizer <ceh329@gmail.com> wrote:
>> Personally, I prefer to not use a lambda:
>>
>> def name_version(elem):
>>     return elem['name'], LooseVersion(elem['version'])
>>
>> result = sorted(mylist, key=name_version, reverse=True)
>
> Peter, thank you. Me being new to Python why don't you prefer to use a lambda?

Using lambda is fine if it's really clear what's going on (usually, if
it's an extremely simple function), but if your expression goes across
multiple lines because of the function parameter, it's usually simpler
to break the function out into a separate def statement and then use
that. There's ultimately no difference[1] between a lambda function
and a def function, apart from the fact that a function created with
lambda always has the name "<lambda>".

ChrisA

[1] Modulo bugs, eg a weird edge case with lambda and yield; certainly
no intentional difference.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-02 10:17 -0800
  Re: Sort list of dictionaries Emile van Sebille <emile@fenx.com> - 2015-03-02 10:31 -0800
    Re: Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-02 10:38 -0800
      Re: Sort list of dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-02 11:55 -0700
      Re: Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-02 10:58 -0800
        Re: Sort list of dictionaries Peter Otten <__peter__@web.de> - 2015-03-02 20:23 +0100
          Re: Sort list of dictionaries Charles Heizer <ceh329@gmail.com> - 2015-03-03 07:56 -0800
            Re: Sort list of dictionaries Chris Angelico <rosuav@gmail.com> - 2015-03-04 03:09 +1100
              Re: Sort list of dictionaries Paul  Moore <p.f.moore@gmail.com> - 2015-03-03 08:48 -0800
            Re: Sort list of dictionaries Peter Otten <__peter__@web.de> - 2015-03-03 18:44 +0100
      Re: Sort list of dictionaries Dave Angel <davea@davea.name> - 2015-03-02 13:59 -0500
      Re: Sort list of dictionaries Jason Friedman <jsf80238@gmail.com> - 2015-03-02 22:33 -0700
      Re: Sort list of dictionaries Chris Angelico <rosuav@gmail.com> - 2015-03-03 18:07 +1100
      Re: Sort list of dictionaries Jason Friedman <jsf80238@gmail.com> - 2015-03-03 07:45 -0700
      Re: Sort list of dictionaries Chris Angelico <rosuav@gmail.com> - 2015-03-04 01:50 +1100
  Re: Sort list of dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-03 18:55 +1100

csiph-web