I wrote a code in python which looks like:
maplist=[{}]*11
mylist=[0]*11
maplist[0]['this']=1
print maplist
When I print maplist the output is :
[{'this': 1}, {'this': 1}, {'this': 1}, {'this': 1}, {'this': 1}, {'this': 1}, {'this': 1}, {'this': 1}, {'this': 1}, {'this': 1}, {'this': 1}]
Expected is:
[{'this': 1}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
rather than only the first element of the list should have this key in the map. What is causing this problem?
When you do the following:
you end up with eleven references to the same dictionary. This means that when you modify one dictionary, they all appear to change.
To fix, replace that line with:
Note that, since
0is a scalar, the next line is fine as it is: