[Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.

Emile van Sebille emile at fenx.com
Tue Nov 3 16:55:15 CET 2009


On 11/3/2009 7:20 AM Robert Berman said...
> 
> In [69]: l1=[(0,0)] * 4
> 
> In [70]: l1
> Out[70]: [(0, 0), (0, 0), (0, 0), (0, 0)]
> 
> In [71]: l1[2][0]
> Out[71]: 0
> 
> In [72]: l1[2][0] = 3
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> 
> /home/bermanrl/<ipython console> in <module>()
> 
> TypeError: 'tuple' object does not support item assignment

Start out slower...

a = (1,2)
a[1]=3

You can't change a tuple.  You can create a new tuple, or you can change 
the content of an item help in a tuple, but you can't change the content 
of the tuple itself.  Try the following then see if it helps answer your 
questions...

b = [1]

a = (1,b)

b[0]=2

print a

a[1][0]=3

print a

Emile

> 
> First question, is the error referring to the assignment (3) or the 
> index [2][0]. I think it is the index but if that is the case why does 
> l1[2][0] produce the value assigned to that location and not the same 
> error message.
> 
> Second question, I do know that l1[2] = 3,1 will work. Does this mean I 
> must know the value of both items in l1[2] before I change either value. 
> I guess the correct question is how do I change or set the value of 
> l1[0][1] when I specifically mean the second item of an element of a 2D 
> array?
> 
> I have read numerous explanations of this problem thanks to Google; but 
> no real explanation of setting of one element of the pair without 
> setting the second element of the pair as well.
> 
> For whatever glimmers of clarity anyone can offer. I thank you.
> 
> Robert
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list