[Tutor] 2D list assignment

Bob Gailer bgailer at alum.rpi.edu
Sat Mar 13 20:14:43 EST 2004

At 07:55 AM 3/12/2004, Shitiz Bansal wrote:
>This is a sample python code:
>  a[1][1]=4
>Traceback (most recent call last):
>   File "<pyshell#2>", line 1, in -toplevel-
>     a[1][1]=4
>TypeError: object doesn't support item assignment


 >>> a=((2,4),(4,2))
 >>> type(a[1])
<type 'tuple'>

Look up 3.2 The standard type hierarchy in the Language Reference. Here you 
find that tuples are immutable sequences, and:
"Immutable sequences
An object of an immutable sequence type cannot change once it is created. 
(If the object contains references to other objects, these other objects 
may be mutable and may be changed; however, the collection of objects 
directly referenced by an immutable object cannot change.) "

Now try:
 >>> a=([2,4],[4,2])
 >>> type(a[1])
<type 'list'>
 >>> a[1][1]=4
 >>> a
([2, 4], [4, 4])

>Is it a bug or am i missing the point?
>Do you Yahoo!?
>Yahoo! Search - <http://search.yahoo.com/?fr=ad-mailsig-home>Find what 
>you're looking for faster.
>Tutor maillist  -  Tutor at python.org

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040313/0c7490c3/attachment.html

More information about the Tutor mailing list