fast way of turning a list of integers<256 into a string

Andrew McGregor andrew at indranet.co.nz
Mon Jan 13 18:00:22 EST 2003


--On Monday, January 13, 2003 14:44:51 -0800 "Jonathan P." 
<jbperez808 at yahoo.com> wrote:

> s=''
> for i in range(1,255):
>   s+=chr(i)
>
> has horrible performance (primarily due to string
> immutability I think).  Is there a function to do
> the ff. instead:
>
> x=range(1,255)
> list2str(x)
>
> and have x become a string?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

''.join(map(chr, range(1,255))) is pretty fast, as is

import array
array.array('B', range(1,255)).tostring()

I haven't profiled to see which is faster.

Andrew







More information about the Python-list mailing list