Fastest way to loop through each digit in a number?

Rune Strand rst at
Mon Sep 6 05:55:37 CEST 2004

Paul Rubin <> wrote:

>You could try timing something like
>  while n:
>    n,d = divmod(n, 10)
>    foo(d)
>That processes the digits in reverse order, of course.

It's faster! But Roy Smiths modulus (%) method is even faster. The
order does matter, but even when appending d to a list inside the loop
and reversing it when done, your methods are  faster than my initial
groks ;-) 

More information about the Python-list mailing list