[Tutor] List comprehension question

Hugo Arts hugo.yoshi at gmail.com
Mon Nov 8 00:53:06 CET 2010


On Mon, Nov 8, 2010 at 12:36 AM, Richard D. Moores <rdmoores at gmail.com> wrote:
> def proper_divisors(n):
>     """
>     Return the sum of the proper divisors of positive integer n
>     """
>     return sum([x for x in range(1,n) if int(n/x) == n/x])
>
> The list comprehension is this function is inefficient in that it computes
> n/x twice. I'd like to do an  a = n/x and use a in
> "if int(a) == a", but I don't know how.
>

You can't do that inside a list comprehension. Either get rid of the
comprehension and do a regular loop, or get rid of the n/x expression.

I'd suggest replacing the whole check with x % n == 0. n is a proper
divisor of x if there is no remainder after division, after all. This
also means you won't have to do a cast, which tend to be fairly
expensive.

On another note, getting rid of the list comprehension and using a
generator expression will be even faster, since you won't have to
build the list.

Hugo


More information about the Tutor mailing list