[Tutor] Problems changing a list item in place ?

Jeff Shannon jeff@ccvcorp.com
Mon, 11 Feb 2002 11:00:05 -0800

> "Chris McCormick" <cmccormick@thestate.com> wrote:
> Hello all,
>     I have a program with a two-dimensional list called map.  After it has already been populated, I want to do this:
>      map[y_box][x_box][0] = selected_tile
>      But I get this error:
>    File "map_edit.py", line 270, in main
>     map[y_box][x_box][0] = selected_tile                        # Change value of the tile
> TypeError: object doesn't support item assignment
>   y_box and x_box are indexes; the statement "print map[y_box][x_box]" works fine.  selected_tile is just an integer, though it shouldn't matter.
> I thought you could change list items in-place by assignment.  Are multi-dimensional lists different?

No, but your assignment statement is trying to access a *third* dimension of your list.  The question is, what is present at map[y_box][x_box] ?? (In
other words, what's printed out when you 'print map[y_box][x_box]'?)  My guess is that you're trying to assign to an individual element of an
integer, which doesn't have individual elements.  :)  If that's the case, then all you need to do is eliminate the [0] on the left-hand-side of your
assignment--in other words, use this:

map[y_box][x_box] = selected_tile

If this doesn't do what you expect, then try posting a bit more code--specifically, let us see what you've actually got in your 2D list.

Jeff Shannon
Credit International