ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.
by Andre Engels other posts by this author
Nov 3 2009 7:59AM messages near this date
Re: [Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'. | Re: [Tutor] Being beaten up by a tuple that's an integer thats a tuple that may be an unknown 'thing'.
On Tue, Nov 3, 2009 at 4:20 PM, Robert Berman <bermanrl@[...].com>  wrote:

> 
>    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
> 
>  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.
> 

Tuples are immutable types. Thus it is not possible to change one of the
values of a tuple (or even of changing both of them). The only thing you can
do, is create a new tuple, and put that in the same place in the list. In
your example, when you do l1[2][0] = 3, you try to change the tuple l1[2],
which is impossible.

To do what you want to do, you have to create a new array with the same
second but different first value, and put that array in l1[2], that is:

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




-- 
André Engels, andreengels@gmail.com
Thread:
Robert Berman
Bob Gailer
Andre Engels
Mark Tolonen
Emile van Sebille
Wayne Werner
Robert Berman

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved