[Python-ideas] back-references in comprehensions
Jacob Holm
jh at improva.dk
Sat Oct 8 21:02:35 CEST 2011
On 2011-10-08 19:48, yoch melka wrote:
> I would like to use backreferences in list comprehensions (or other
> comprehensions), such as :
>
> [[elt for elt in lst if elt] for lst in matrix if \{1}]
> # \{1} is back-reference to filter result of the
> # first comprehension ([elt for elt in lst if elt])
>
> It would be possible to do this ?
I don't think the syntax you propose is going to fly, but a similar
feature has been discussed before on this list. I prefer this version:
[[elt for elt in lst if elt] as r for lst in matrix if r]
But I don't have much hope that it will ever get in.
FWIW, the example can already be written in several ways in current
python, e.g.:
1) [r for lst in matrix for r in ([elt for elt in lst if elt],) if r]
2) filter(None, ([elt for elt in lst if elt] for lst in matrix))
HTH
- Jacob
More information about the Python-ideas
mailing list