Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have this 7x7 two-dimensional array:

l=[[1, 1, 1, 1, 1, 1, 1],
  [1, 0, 2, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 1],
  [1, 0, 0, 0, 0, 0, 1],
  [1, 1, 1, 1, 1, 1, 1]]

As you can see, l[1][2]=2. When I print it, the element is printed correctly. No problem here. But when I try to change it from "2" to "3" or any other number, the program changes all the elements on that column (in this case the 3rd column) except for the first and last ones. For example, if I type this code:

l[1][2]=5

and then print the two-dimensional array, I get this:

l=[[1, 1, 1, 1, 1, 1, 1],
  [1, 0, 5, 0, 0, 0, 1],
  [1, 0, 5, 0, 0, 0, 1],
  [1, 0, 5, 0, 0, 0, 1],
  [1, 0, 5, 0, 0, 0, 1],
  [1, 0, 5, 0, 0, 0, 1],
  [1, 1, 1, 1, 1, 1, 1]]

This happens with every element that I choose. Instead of changing only that element, it changes the entire column. Does anyone know what might be the problem? Thank you!

share|improve this question

closed as off-topic by lpapp, thefourtheye, aga, Roman C, Aleksey Izmailov Mar 5 '14 at 19:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – thefourtheye, aga, Roman C, Aleksey Izmailov
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Not reproducible. – thefourtheye Jan 10 '14 at 4:21
1  
I cannot reproduce it. Could you please run all this yourself in the python interpreter? Which python are you using? – lpapp Jan 10 '14 at 4:21
    
This makes no sense, even if you have a NumPy matrix or multiple references to the same list. Could you please show us an actual interpreter session? – user2357112 Jan 10 '14 at 4:23
    
Can you show us how you actually construct the list to start? I assume that you accidentally are storing multiple references to the same list. – mgilson Jan 10 '14 at 4:23
    
@mgilson: Likely, but that still wouldn't produce the output shown. This is probably because the output shown isn't what the OP actually got. – user2357112 Jan 10 '14 at 4:25
up vote 10 down vote accepted

I'm gonna take a stab at this one even though the behavior you describe (as you've described it) isn't possible.

If you create a list, you need to make sure that each sublist is a different list. Consider:

a = []
b = [a, a]

Here I've created a list where both of the sublists are the exact same list. If I change one, it will show up in both. e.g.:

>>> a = []
>>> b = [a, a]
>>> b[0].append(1)
>>> b
[[1], [1]]

you'll frequently see this behavior with a list initialized using the * operator:

a = [[None]*7]*7

e.g.

>>> a = [[None]*7]*7
>>> a
[[None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None]]
>>> a[0][1] = 3
>>> a
[[None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None]]

The fix is to not use the * 7 on the outer list (the inner list is OK since None is immutable):

a = [[None]*7 for _ in range(7)]

e.g.:

>>> a = [[None]*7 for _ in range(7)]
>>> a[0][1] = 3
>>> a
[[None, 3, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None]]
share|improve this answer
    
Thank you. That was the problem. I solved it using your suggestion. – Joker Jan 10 '14 at 4:40

You've constructed your list wrong.

The middle items are all referring to the same list, so updating one causes the change to be reflected in the others

If you show the code you're using to construct the list, I can show you how to fix it.

Alternatively

l = [sublist[:] for sublist in l]

before you begin modifying the lists will decouple all those into new lists

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.