That's because you associated all keys of the outer dictionary with the same inner dictionary. You first constructed a dictionary with dict.fromkeys(y,0)
, and then you associate that dictionary with all the keys with: dict.fromkeys(x,...)
.
A way to construct the dictionary you want is for instance dictionary comprehension:
zx = {k: dict.fromkeys(y,0) for k in x}
Although this looks quite the same it is not: here, we will for every k
in x
evaluate dict.fromkeys(y,0)
again. As a result, the constructed dictionaries will all he equivalent, but not the same object.
Now we obtain the expected:
>>> x=[1,2,3,4,5]
>>> y=[7,8,9,10,11]
>>> zx = {k: dict.fromkeys(y,0) for k in x}
>>> zx
{1: {8: 0, 9: 0, 10: 0, 11: 0, 7: 0}, 2: {8: 0, 9: 0, 10: 0, 11: 0, 7: 0}, 3: {8: 0, 9: 0, 10: 0, 11: 0, 7: 0}, 4: {8: 0, 9: 0, 10: 0, 11: 0, 7: 0}, 5: {8: 0, 9: 0, 10: 0, 11: 0, 7: 0}}
>>> zx[1][8]+=1
>>> zx
{1: {8: 1, 9: 0, 10: 0, 11: 0, 7: 0}, 2: {8: 0, 9: 0, 10: 0, 11: 0, 7: 0}, 3: {8: 0, 9: 0, 10: 0, 11: 0, 7: 0}, 4: {8: 0, 9: 0, 10: 0, 11: 0, 7: 0}, 5: {8: 0, 9: 0, 10: 0, 11: 0, 7: 0}}