List comprehension vs generator expression memory allocation

Duncan Booth duncan.booth at invalid.invalid
Sun Sep 20 17:04:49 CEST 2009


candide <candide at free.invalid> wrote:

> Each of the following two functions mult1() and mult2() solves the
> question :
> 
> 
> # -----------------------------------------
> def mult1(a,b,m):
>     return (x for x in range(a,b)[(m-a%m)%m:b:m])
> 
> def mult2(a,b,m):
>     return range(a,b)[(m-a%m)%m:b:m]
> # -----------------------------------------
> 
> 
Why are you slicing the result of range? Why not just pass appropriate 
arguments to range or xrange directly?

def f(a,b,m):
	return xrange((a+m-1)//m*m, b, m)




More information about the Python-list mailing list