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: 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: <946797be-10e6-433b-9411-2db0d5697ac8@googlegroups.com> <8ccd65d8-3f1f-41ac-9092-9b0832d2fc49@googlegroups.com> <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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Wed, Mar 4, 2015 at 2:56 AM, Charles Heizer 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 "". ChrisA [1] Modulo bugs, eg a weird edge case with lambda and yield; certainly no intentional difference.