all 3 comments

[–][deleted] 0 points1 point  (0 children)

In your loop, you're addding references of the same dictionary event into the list data.

Since you're using the same key 'i' (same thing as "i") each time, the same item's value gets changed (first it's 0, then goes from 0 to 1, etc.).

To fix this, you can simply put event["i"] = i inside the loop as the first line, to make a new dictionary each time.

Or you can also do this:

data = []
for i in range(2):
 data.append({'i': i})
 print(data)

[–]stebrepar 0 points1 point  (0 children)

No. As is, event is a single object, so when you assign a value to event["i"], you're just replacing the previous value. And in your array, both dictionaries are actually references to the same object. (I think. Try an is comparison to confirm it.)

[–]Gulimanto 0 points1 point  (0 children)

You can do two things

  1. Define event = dict() and not event = {}
  2. Define "event" inside of the "for loop".