[Python-ideas] More power in list comprehensions with the 'as' keyword

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Thu Aug 28 11:04:41 CEST 2008


2008/8/28 Cesare Di Mauro <cesare.dimauro at a-tono.com>:

> A solution could be this:
>
> [stripped for l in text.split('\n') with l.strip() as stripped if stripped != '']
>
> so that you can keep both values (l and l.strip()) too.

In Haskell this would be (I translate only the list comprehension
structure, leaving expressions in Python syntax):

[stripped | l <- text.split('\n'), let stripped = l.strip(), stripped != '']

Python borrowed 2 out of 3 kinds of list comprehension constructs.

-- 
Marcin Kowalczyk
qrczak at knm.org.pl
http://qrnik.knm.org.pl/~qrczak/



More information about the Python-ideas mailing list