How to write this as a list comprehension?

Matěj Cepl matej at ceplovi.cz
Sat Jan 18 05:57:27 EST 2014


-----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-----



More information about the Python-list mailing list