Appending a dictionary to a list in a loop

PythonListDictionaryAppend

Python Problem Overview


I am trying to take a dictionary and append it to a list. The dictionary then changes values and then is appended again in a loop. It seems that every time I do this, all the dictionaries in the list change their values to match the one that was just appended.

For example:

>>> dict = {}
>>> list = []
>>> for x in range(0,100):
...     dict[1] = x
...     list.append(dict)
... 
>>> print list

I would assume the result would be [{1:1}, {1:2}, {1:3}... {1:98}, {1:99}] but instead I got:

[{1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}, {1: 99}]

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

When you create the adict dictionary outside of the loop, you are appending the same dict to your alist list. It means that all the copies point to the same dictionary and you are getting the last value {1:99} every time. Just create every dictionary inside the loop and now you have your 100 different dictionaries.

alist = []
for x in range(100):
	adict = {1:x}
	alist.append(adict)
print(alist)

Solution 3 - Python

Just put dict = {} inside the loop.

>>> dict = {}
>>> list = []
>>> for x in range(0, 100):
       dict[1] = x
       list.append(dict)
       dict = {}

>>> print list

Solution 4 - Python

You can also use zip and list comprehension to do what you need.

If you want the dict values to start at one use range(1,100)

l = [dict(zip([1],[x])) for x in range(1,100)]

Solution 5 - Python

Let's say d is your dictionary. Here, if you do d.copy(). It returns shallow copy that doesn't work when you have nested dictionary into d dictionary. To overcome from this issue, we have to use deepcopy.

from copy import deepcopy
list.append(deepcopy(d))

It works perfectly !!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAcoopView Question on Stackoverflow
Solution 1 - PythonMartijn PietersView Answer on Stackoverflow
Solution 2 - PythonchapeloView Answer on Stackoverflow
Solution 3 - PythondocjagView Answer on Stackoverflow
Solution 4 - PythonPadraic CunninghamView Answer on Stackoverflow
Solution 5 - PythoniNikkzView Answer on Stackoverflow