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

Dave Reed dreed at capital.edu
Mon Jan 13 18:11:40 EST 2003


On Monday 13 January 2003 17:44, Jonathan P. 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?


Use map or list comprehensions to convert all the elements using chr
and then use ''.join on the list.

x = range(1,256)
x = map(chr, x)
x = ''.join(x)

or in one step:

x = ''.join(map(chr, range(1, 256)))

HTH,
Dave






More information about the Python-list mailing list