0

I have a list of dictionaries that I am updating through a for loop. However, it seems calling update() on a dictionary within the list is resulting in every dictionary in the list being update with the most current loop's values.

While the original code is more complex, I stripped away all the unrelated stuff. Below is a test snippet I wrote just to see if it's the issue of the other pieces in my code or just the update().

test = [{}] * 3

for i in range(0, 3):
        test[i].update({'test': i, 'val': i+1})
print(test)

The expected result is:

[{'test': 0, 'val': 1}, {'test': 1, 'val': 2}, {'test': 2, 'val': 3}]

But instead I get:

[{'test': 2, 'val': 3}, {'test': 2, 'val': 3}, {'test': 2, 'val': 3}]

Not entirely sure why this is the case. If anyone could help I would really appreciate it. Thank you!

Mark Wong
  • 1
  • 1

0 Answers0