I have read these posts here explaining exactly the problem I am having, but none of the solutions are working:
Changing one list unexpectedly changes another, too
Changing One dict value changes all values
I have a specific shape of 2D array I need to initialize the entries in a dictionary with. Here is how I have done it:
empty = []
for x in range(2):
empty.append([])
for y in range(2):
empty[x].append(False)
status = {k:[] for k in ["a", "b", "c"]}
status["a"] = list(empty)
status["b"] = list(empty)
status["c"] = list(empty)
print(status)
status["a"][0][0] = True
print(status)
(Shape of list simplified for example)
This prints:
{'a': [[False, False], [False, False]], 'b': [[False, False], [False, False]], 'c': [[False, False], [False, False]]}
{'a': [[True, False], [False, False]], 'b': [[True, False], [False, False]], 'c': [[True, False], [False, False]]}
As you can see, setting one of the lists values changes all of the lists. I do not want this, I want them to be separate lists (in one dictionary) with different values.
Initially, I thought I had done the old newlist = oldlist
blunder where I set newlist to the same object as oldlist, but nope. As you can see in my code, I am making separate lists using newlist = list(oldlist)
. I have also tried newlist = oldlist[:]
, newlist = oldlist.copy()
, etc.
What am I missing?