1

I'm trying to change the value of one cell in a 2D python array:

[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

which I generated from

m=3*[[0]*3]
for y in xrange(0,3):
    for x in xrange(0,3):
        m[y][x]=x

I want to replace m[0][0] with 5 so that it looks like

[[5, 1, 2], [0, 1, 2], [0, 1, 2]]

but instead it is

[[5, 1, 2], [5, 1, 2], [5, 1, 2]]

when i use

m[0][0]=5

what's going on?

mango
  • 543
  • 1
  • 5
  • 14
  • 1
    Please see the linked duplicate; multiplication as in `m=3*[[0]*3]` doesn't work like you think it does. :-) – DSM Jan 01 '15 at 05:10
  • 1
    @DSM thanks. what do i do with a question when there's a duplicate? – mango Jan 01 '15 at 05:12
  • 3
    No need to do anything! I linked this question to the other, so now anyone who describes the problem using the same words you did will have an easier time finding an answer. – DSM Jan 01 '15 at 05:16
  • this question gets worded a new way every time because theres no way to know whats happening the first time you encounter it – jamylak Jan 01 '15 at 05:49

0 Answers0