[Tutor] Intializing Lists - Did it again!!

Zak Arntson zak at harlekin-maus.com
Fri Aug 22 16:10:44 EDT 2003


Can you believe it? I've been fiddling with pygame lately, and had to
initialize a list of lists (to represent a 2d set of map tiles).

So I then go and stupidly do the following:
tilemap = [[0] * 10] * 10

Now, repeat after me: This creates ONE [0] * 10 list and filles the outer
list with pointers to that list. I.e.:

###

>>> a = [['a'] * 3] * 3
>>> a
[['a', 'a', 'a'], ['a', 'a', 'a'], ['a', 'a', 'a']]
>>> a[0][1] = 2
>>> a
[['a', 2, 'a'], ['a', 2, 'a'], ['a', 2, 'a']]

###

Man, didn't I JUST ask the Tutor list about this very same thing? Anyhow,
thought I'd reiterate such an important thing.

PS - I was able to throw together an scrolling map with isometric,
overlapping (big rocks and such) tiles ... all in two lunches ... oh, and
the mouse scrolls the map a la Starcraft. Is Python awesome, or what?

---
Zak Arntson
www.harlekin-maus.com - Games - Lots of 'em



More information about the Tutor mailing list