Is there a more elegant way to spell this?
Rustom Mody
rustompmody at gmail.com
Tue Jan 27 09:08:29 EST 2015
On Tuesday, January 27, 2015 at 6:45:41 PM UTC+5:30, Neal Becker wrote:
> Is there a more elegant way to spell this?
>
> for x in [_ for _ in seq if some_predicate]:
Depends on what follows the ':'
In the trivial case all thats outside the comprehension can be dropped:
>>> [x for x in [y for y in range(10) if y % 2 == 0]]
[0, 2, 4, 6, 8]
>>> [y for y in range(10) if y % 2 == 0]
[0, 2, 4, 6, 8]
>>>
Or
>>> [x*x for x in [y for y in range(10) if y % 2 == 0]]
[0, 4, 16, 36, 64]
>>> [y*y for y in range(10) if y % 2 == 0]
[0, 4, 16, 36, 64]
>>>
>
>
> --
> -- Those who don't understand recursion are doomed to repeat it
Ha!
More information about the Python-list
mailing list