It looks like they have same memory address, so modifying one modifies them all, and this is because they are a only reference.
Here is a better explanation:
Your first line creates your object with integers keys and empty object as a value for all of them.
yeartonametolist = dict.fromkeys(range(2007,2017),{})
Now at this point, if you use the id()
function like this
id(yeartonametolist[2016]) #4294024972L
id(yeartonametolist[2015]) #4294024972L
It is the same id, so if you do
yeartonametolist[2007]["a"] = 1
yeartonametolist[2008]["b"] = 2
you are changing the same object.
You can see it as well if you change the {}
to object()
test = dict.fromkeys(range(2007,2017), object() )
print test
Output:
{
2016: <object object at 0xfff964d8>,
2007: <object object at 0xfff964d8>,
2008: <object object at 0xfff964d8>,
2009: <object object at 0xfff964d8>,
2010: <object object at 0xfff964d8>,
2011: <object object at 0xfff964d8>,
2012: <object object at 0xfff964d8>,
2013: <object object at 0xfff964d8>,
2014: <object object at 0xfff964d8>,
2015: <object object at 0xfff964d8>
}
The value for each key points to the same memory address.