list of lists, object doesn't support item assignment?

Rainer Deyke root at rainerdeyke.com
Fri Jun 29 00:55:37 EDT 2001


"Manfred Bartz" <mdadd329ea at xix.com> wrote in message
news:m2vglfkitn.fsf at reddwarf.xix.net...
>
> >>> xy = (1,2),(2,2)
> >>> print xy, xy[0]
> ((1, 2), (2, 2)) (1, 2)
> >>> xy[1]=(3,4)
> TypeError: object doesn't support item assignment
>
> Is there a way to assign the list (3,4)

That's not a list.

> to an element of xy?

No.

> What am I missing?

The difference between lists and tuples.  Lists use are written '[1,2]' and
are mutable.  Tuples are written '(1,2)' or just '1,2' and are immutable.
This work:

xy = [[1, 2], [2, 3]]
xy[1] = [3, 4]

This also work:

xy = [(1, 2), (2, 3)]
xy[1] = (3, 4)


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list