Appending a dictionary to a list in a loop

You need to append a copy, otherwise you are just adding references to the same dictionary over and over again:

yourlist.append(yourdict.copy())

I used yourdict and yourlist instead of dict and list; you don’t want to mask the built-in types.

Leave a Comment