[FP] Rewriting name bindings as expressions

Greg Ewing see at my.signature
Thu May 24 21:59:10 EDT 2001


Richard Davies wrote:
> 
> ans = []
> for x in range(10):
>    y = x*x
>    ans.append(y+1)
> 
> We can rewrite this as a list comprehension
> but ... how do we do so whilst binding y to x*x

ans = [y+1 for x in range(10) for y in (x*x,)]

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list