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]]