Fredrik Lundh wrote: > since you know the length, you can preallocate the list > > def iters3(n): > L = [None]*n > for i in xrange(n): > L[i] = chr(i%64) > return "".join(L) > > or use a preallocated array > > def iters4(n): > L = array.array("B", [0])*n > for i in xrange(n): > L[i] = i%64 > return L.tostring()
Of course. I was just trying to make a point about string accumulation being O(n) and not O(n^2). You can get better factors with other solutions like the ones you propose, but even the "simple" implementation (and most readable and Pythonic, IMHO) behaves "well" under CPython 2.4. -- Giovanni Bajo -- http://mail.python.org/mailman/listinfo/python-list