Why are strings immutable?

Jeff Shannon jeff at ccvcorp.com
Mon Aug 23 19:34:11 EDT 2004


Larry Bates wrote:

>Your example:
>
>List = [ ]
>for i in range(20000):
>    Word = DoSomeProcessing()
>    List.extend(list(Word))
>Str = ''.join(List)
>
>
>will work as:
>
>words=[]
>for i in xrange(20000):
>    word = DoSomeProcessing()
>    words.append(word)
>
>word_string = ' '.join(words)
>  
>

Or even (using a list comp):

words = ' '.join( [DoSomeProcessing() for i in xrange(20000)] )

Though I have to wonder what you're doing with a 20,000 word string, 
built programmatically word-by-word.  While I don't know what you're 
doing, here, the way you're building it seems to suggest to me that a 
list or dictionary may actually be a more natural way to handle your data.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list