Python Interview Questions

Rhamphoryncus rhamph at
Wed Oct 31 23:31:44 CET 2007

On Oct 31, 2:58 am, konryd <kon... at> wrote:
> > - string they use "+=" or do they build a list
> >    and use .join() to recombine them efficiently
> I'm not dead sure about that, but I heard recently that python's been
> optimized for that behaviour. That means: using += is almost as fast
> as joining list. Besides, "+=" is more obvious than the other choice
> and since performance is not a problem until it's a problem, I'd
> rather use it. Anyway: I wouldn't pick it for an interview question.

Concatenating strings using += will often perform quadratically with
the number of concatenations.  Your testing will likely use a small
number and not take a noticeable amount of time.  Then it'll get
deployed and someone will plug in a much larger number, wondering why
the program is so slow.  The recent optimizations just make it more

+= shouldn't be an obvious choice for sequences.  If it's mutable,
use .append().  If it's immutable, build up in a mutable sequence,
then convert.

Adam Olsen, aka Rhamphoryncus

More information about the Python-list mailing list