Half-baked idea: list comprehensions with "while"
Tim Chase
python.list at tim.thechases.com
Fri Apr 27 09:14:24 EDT 2012
On 04/27/12 07:23, Chris Angelico wrote:
> On Fri, Apr 27, 2012 at 10:17 PM, John O'Hagan<research at johnohagan.com> wrote:
>> results = [x = expensive_call(i) for i in iterable if condition(x)]
>
> Nest it:
>
> results = [x for x in (expensive_call(i) for i in iterable) if condition(x)]
While it's what I do in cases like this, the nesting is not
nearly as readable as John's suggested syntax. Granted, the
ability to do assignments in such a context opens up a whole can
of unpythonic worms, so I'd suggest the following for clarity:
temp = (expensive_call(i) for i in iterable)
results = [x for x in temp if condition(x)]
-tkc
More information about the Python-list
mailing list