[Python-ideas] Assignments in list/generator expressions

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Apr 12 02:14:37 CEST 2011


Nick Coghlan wrote:

> This one is tricky, since the assignment is *after* the for loop, but
> *before* the filter condition.

That's why I like the 'letting' form, because it both
reads correctly and matches the order of execution.

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

translates to

   ys = []
   for x in xs:
     y = f(x)
     if y:
       ys.append(y)

-- 
Greg



More information about the Python-ideas mailing list