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)