0
x = []
a = {'sample': True}
x.append(a)
a["sample"] = False
x.append(a)
print(x)

The current result is [{'sample': False}, {'sample': False}].

I need the response to be [{'sample': True}, {'sample': False}].

I've tried x.update({"sample": False}); same effect.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
MR Sample
  • 25
  • 6

1 Answers1

2

You are appending the 'pointer' (python object) of dictionary, not a copy.

You can try:

def copyof(dico): return {**dico}
a = {'a':True}
x.append(copyof(a))

It will be Ok

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Henry
  • 577
  • 4
  • 9