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


Groups > comp.lang.python > #64224

Re: How to write this as a list comprehension?

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <matej@ceplovi.cz>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'algorithm': 0.04; 'element': 0.07; 'nicely': 0.07; 'pgp': 0.09; 'subject:skip:c 10': 0.09; 'subject:How': 0.10; '(gnu/linux)': 0.16; '-----begin': 0.16; '-----end': 0.16; 'gnupg': 0.16; 'hash:': 0.16; 'lambda': 0.16; 'name)': 0.16; 'sha1': 0.16; 'variables;': 0.16; 'applies': 0.16; 'wrote:': 0.18; 'message-----': 0.19; 'example': 0.22; 'header:User-Agent:1': 0.23; 'beauty': 0.24; 'instead.': 0.24; '(or': 0.24; 'somewhere': 0.26; 'signed': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'subject:list': 0.30; 'skip:i 60': 0.31; 'front': 0.32; 'stuff': 0.32; 'created': 0.35; 'but': 0.35; 'add': 0.35; 'version:': 0.36; 'subject:?': 0.36; 'should': 0.36; 'list': 0.37; 'list.': 0.37; 'being': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'short': 0.38; 'anything': 0.39; 'does': 0.39; 'embedded': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'commands': 0.60; 'then,': 0.60; 'new': 0.61; 'header:Message-Id:1': 0.63; 'show': 0.63; 'name': 0.63; 'more': 0.64; 'here': 0.66; 'subject:this': 0.83; '(always': 0.84; 'filtered': 0.84; 'processed,': 0.84; 'remember,': 0.93
To python-list@python.org
Subject Re: How to write this as a list comprehension?
In-Reply-To <m2sism42n8.fsf@cochabamba.vanoostrum.org>
References <m2sism42n8.fsf@cochabamba.vanoostrum.org>
Date Sat, 18 Jan 2014 11:57:27 +0100
User-Agent slrn/1.0.1 (Linux)
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
From matej@ceplovi.cz (Matěj Cepl)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.5675.1390042655.18130.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1390042655 news.xs4all.nl 2854 [2001:888:2000:d::a6]:55837
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64224

Show key headers only | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2014-01-17, 23:19 GMT, you wrote:
> But defining the auxfunc takes away the elegance of a list 
> comprehension.

Au contraire! Remember, that brevity is the sister of talent.  

I would definitively vote for 

    labels = [make_label(then, name) for then, name in mylist]

(always use descriptive names of functions and variables; 
auxfunc is a short way to the hell)

Beauty of the list comprehensions is that they show nicely what 
list is being processed, how it is filtered (if at all), and 
what we do with each element of the generated list. Anything you 
add to this simplicity is wrong. Whenever you start to feel you 
are missing some methods how to stuff more commands into 
a comprehension (or for example multiple embedded ones), you 
should start new function.

The same rule applies here as with any other lambda function 
(because these are in fact lambda functions): the best way how 
to write lambda is to write algorithm somewhere on the side, 
describe what this function does in one word, then add `def` in 
front of that name, and use so created named function instead.

Best,

Matěj

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iD8DBQFS2l4X4J/vJdlkhKwRAjEgAJ4n1OuANYlVFzlgBZ0f1uMhO/t36gCfdFjE
VmYDJ+F7aN0khzvlY50i0iA=
=Trcc
-----END PGP SIGNATURE-----

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


Thread

How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-18 00:19 +0100
  Re: How to write this as a list comprehension? Dan Stromberg <drsalists@gmail.com> - 2014-01-17 15:49 -0800
  Re: How to write this as a list comprehension? Rustom Mody <rustompmody@gmail.com> - 2014-01-17 19:25 -0800
    Re: How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-18 11:00 +0100
  Re: How to write this as a list comprehension? Peter Otten <__peter__@web.de> - 2014-01-18 09:36 +0100
    Re: How to write this as a list comprehension? Rustom Mody <rustompmody@gmail.com> - 2014-01-18 07:20 -0800
      Re: How to write this as a list comprehension? Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-01-18 18:00 +0200
        Re: How to write this as a list comprehension? "Rhodri James" <rhodri@wildebst.org.uk> - 2014-01-19 23:41 +0000
          Re: How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-20 12:02 +0100
            Re: How to write this as a list comprehension? Rustom Mody <rustompmody@gmail.com> - 2014-01-20 03:47 -0800
  Re: How to write this as a list comprehension? matej@ceplovi.cz (Matěj Cepl) - 2014-01-18 11:57 +0100
  Re: How to write this as a list comprehension? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-01-18 12:53 +0100
    Re: How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-18 18:40 +0100
  Re: How to write this as a list comprehension? John Allsup <pydev@allsup.co> - 2014-01-19 01:24 +0000
    Re: How to write this as a list comprehension? Piet van Oostrum <piet@vanoostrum.org> - 2014-01-19 23:06 +0100

csiph-web