[Tutor] Changing the value in a list

Steve Willoughby steve at alchemy.com
Thu Mar 11 16:46:09 CET 2010

On Thu, Mar 11, 2010 at 10:32:45AM -0500, Ken G. wrote:
>    list = ['physics', 'chemistry', 1997, 2000]

This is a list of 4 elements, and lists allow elements to
be changed, so

>    list[2] = 2010

Will change the value of list to be ['physics','chemistry',1997,2010]

>    line = [2010020820841134]

Is this one right, or did you mean '2010020820841134'?  What is shown is
a list of one element.  Assuming you meant it to be a string, that would
give the error you quote below.

Strings are immutable objects, meaning that once they are created you can't
change them, although you can construct new strings from them, so if you
wanted to make a string which had characters 9 and 10 replaced with "08"
you could say:
  line = line[:9]+"08"+line[11:]

Steve Willoughby    |  Using billion-dollar satellites
steve at alchemy.com   |  to hunt for Tupperware.

More information about the Tutor mailing list