Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Chris Angelico rosuav at gmail.com
Mon Mar 24 12:21:20 CET 2014


On Mon, Mar 24, 2014 at 8:49 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> I'm too lazy to do this the
> right way, but there are at least 99 mentions of "lambda" in the 3.3
> standard library:
>
> steve at runes:/usr/local/lib/python3.3$ grep lambda *.py | wc -l
> 99

I'm not too lazy to do it the right way, but I don't have 3.3 handy,
so I've done it on 3.4 instead. There are 77 instances of lambda nodes
in the files you list there - which are the ones that aren't in
packages. (Note that two instances of lambda on the same line would
count as one in Steven's figure, but as two in mine. Also, his counts
comments. Still, his way's a lot easier to calculate, and it's in the
right ball-park.) Including all subdirectories raises that figure to,
get this, 1230. That's actual uses of the lambda keyword as parsed by
Python. This does include the test suite, though. Removing all files
with "/test/" in the names cuts that figure to only 273. But that's
still two hundred and seventy-three places where the Python standard
library uses lambda - a respectable figure.

ChrisA



More information about the Python-list mailing list