[Tutor] Changing the value in a list
Andre Engels
andreengels at gmail.com
Thu Mar 11 16:44:31 CET 2010
On Thu, Mar 11, 2010 at 4:32 PM, Ken G. <beachkid at insightbb.com> wrote:
> If the following program change list[2] to 2010, replacing 1997 (and it
> does), why doesn't the second program work in changing line[ 9:11] to 20
> from 08?
> FIRST PROGRAM:
>
> list = ['physics', 'chemistry', 1997, 2000]
> print list[2]
> list[2] = 2010
> print list[2]
>
> OUTPUT:
>
> 1997
> 2010
>
> SECOND PROGRAM:
> line = [2010020820841134]
> if line[ 9:11] == "08":
> line[ 9:11] = 20
> print line[ 9:11]
>
> OUTPUT:
>
> "TypeError: 'str' object does not support item assignment."
>
>
> Thanking you all in advance,
First, you seem to have made a mistake in copying your programs: In
the second program to get your error message,
line = [2010020820841134]
should read
line = "2010020820841134"
As for your question: The error message already says it better than I
do: If x is a string, then x[9] = <something> is not allowed, nor is
x[9:11] = <something>.
--
André Engels, andreengels at gmail.com
More information about the Tutor
mailing list