[Python-ideas] Assignments in list/generator expressions

MRAB python at mrabarnett.plus.com
Sat Apr 9 19:55:48 CEST 2011


On 09/04/2011 17:27, Eugene Toder wrote:
> Survey of list comprehension features and syntax:
>
> 1) Structure
> Haskell: [expr | generators], generators = generator (, generator)*
> Python: [expr generators], generators = generator+
> 2) Take
> Haskell: x<- xs
> Python: for x in xs
> 3) Filter
> Haskell: cond
> Python: if cond
> 4) Local definition
> Haskell: let name = expr
> Python: strangely missing
>
> I'd rather add syntax equivalent to Haskell's let, than inventing
> completely new syntax. E.g. with name = expr. Original from the first
> post becomes
>
> ys = [y for x in xs with y = f(x) if y]
>
> but it's more flexible in general.
>
Or possibly:

     ys = [y for x in xs with f(x) as y if y]



More information about the Python-ideas mailing list