I found this bit of code in a module I am working on:
l = opaque_function()
thingys = [x for y in l for x in y]
I can't read this. By experiment, I was able to determe that it is flattening a 2-level nested list, but the syntex is still opaque to me. It has obviously omitted some optional brackets.
>>> l = [[1,2],[3,4]]
>>> [x for y in l for x in y]
[1, 2, 3, 4]
My eyes want to parse it as either: [x for y in [l for x in y] ]
or [ [x for y in l] for x in y ]
, but both of those fail due to y
not being defined.
How should I be reading this?
(I suspect I will feel very embarassed when this is explained.)