Hahhaha i so didn't spot that need to think more . Ah it could be worse though, this guy i used to work for couldnt stop himself typing sausage instead of some word... every time he tried to type this word he'd type sausage lol

Ok i finally got it figured it out, with a little inspiration from you're examples.

No i don't clame its the most efficent function because i can see places that could be improved on i.e. the two str() calls would normally be singular, but since the aim of this example was to write a totaly self contained one liner it had to be so.

improved recursive version

I have played around with this some more, and done some benchmarking. I found that two of the given solutions did not run, and two others gave the wrong answers for long integers (because the conversion to string added an 'L' on the end, which screwed up their indexing).

Of the ones that ran, my version was fastest for integers and grim's comma5 function was slightly faster for longs.

However all the functions so far failed for negative numbers - mine just returned the number unchanged, and all the others (that worked) when given a number like:

-450324598

returned:

-,450,324,598

here is a new version of my recursive function that correctly handles all cases, and has been re-written to be easier to read (this is Python after all, not Perl)

Incidentally, the '//' operator is the 'floor division' operator introduced in 2.2 - for earlier versions of Python use '/'. At the moment they both give the same result, but in the future (I think in 2.4) the '/' operator on integers will convert the answer to a float when necessary.

The switch from integer to long has an impact in Dave's method at which point the string method becomes best. With a very small performance hit to each method the following hybrid makes a good compromise for best overall performance with the current math libraries: