-2

can somebody explain why dictinoray update behaves so, and does not bring results as in approach2? See below code:

myjson1={}
myJsonArray1=[]
myjson2={}
myJsonArray2=[]
print('###Approach2 with append###')
for x in range(1,4):
    myjson1.update({'posnr':x})
    myJsonArray1.append(myjson1)
print(myJsonArray1)
print('###Approach2 without append###')
for x in range(1,4):
    myjson2={'posnr':x}
    myJsonArray2.append(myjson2)
print(myJsonArray2)

so I tried this:

for x in range(1,4):
    myjson1.update({'posnr':x})
    myJsonArray1.append(myjson1)

I was excpecting to have results like this:

for x in range(1,4):
    myjson2={'posnr':x}
    myJsonArray2.append(myjson2)

##UPDATE###

Thanks for the comments, it is now clear.

Another solution to solve the problem in approach1 was to initialize the dictionary in the begining of each iteration, something like this:

for x in range(1,4):
    myjson1={}
    myjson1.update({'posnr':x})
    myJsonArray1.append(myjson1)
print(myJsonArray1)
Troy
  • 27
  • 5
  • In the first example, you reference the dictionary for each element and update it, so each element of the list changes. In the second example, you're overwriting the contents of the dictionary so the list elements are unique. `update` mutates the dictionary in-place. – B Remmelzwaal Aug 26 '23 at 08:21
  • 1
    Appending to a list does ***not*** create a copy of the dict. You’re putting multiple references to the same one dict object into the list. – deceze Aug 26 '23 at 08:23
  • Often you can answer your own questions by simply typing something like `help(dict.update)` into a python console~ – BeRT2me Aug 26 '23 at 08:33

2 Answers2

0

Probably because when you ran myjson1.update({'posnr':x}) in approach 1,

  • .update updated the contents of the dictionary you referenced, which was stored in the myjson1 dictionary variable
  • and in your myJsonArray1 list, the appended myjson1 elements were referencing the same myjson1 dictionary variable

On the other hand when you ran myjson2={'posnr':x} in approach 2

  • you were overwriting the contents of your myjson2 variable during every iteration of your for loop
  • and you were storing the overwritten myjson2 variable contents during each for loop iteration in myJsonArray2
Kai
  • 17
  • 7
0

In your first approach, you are updating the same dictionary myjson1 in each iteration of the loop and appending references to the same dictionary to myJsonArray1. Since dictionaries are mutable objects, when you update myjson1, all references to it (that are already appended to the list) will reflect the changes. That's why you end up with the same dictionary repeated multiple times in myJsonArray1.

myJsonArray1.append(myjson1.copy())

just use copy() while appending to myJsonArray1 to make sure that each dictionary added to the list is a separate copy of myjson1.so, it will help in preventing the repetation of same dictionary.

In the second approach, you're already creating new dictionaries in each iteration, which results in distinct dictionaries being appended to myJsonArray2.

vegan_meat
  • 878
  • 4
  • 10