I don't have a huge list so I'd prefer to use the standard library. I'm trying to initialize a dictionary that holds empty values (so I can do math on them later) but when I change one value, others also change.
Example script
l = ['a', 'b', 'c']
d = {'CategoryA': dict.fromkeys(l, dict.fromkeys(l, 0)),
'CategoryB': dict.fromkeys(l, dict.fromkeys(l, 0)),
'CategoryC': dict.fromkeys(l, dict.fromkeys(l, 0))}
d['CategoryA']['a']['c'] += 1
print d['CategoryA']['a']['c']
print d['CategoryA']['b']['c']
print d['CategoryA']['c']['c']
This prints out 1 three times in a row, but I want it to print out 1 and then 0 twice. It seems the final dictionary values are linked.
I also tried creating two identical lists (uglier code), thinking the lists are linked, but that doesn't fix it, and I get the identical printouts with this uglier code:
l1 = ['a', 'b', 'c']
l2 = ['a', 'b', 'c']
d = {'CategoryA': dict.fromkeys(l1, dict.fromkeys(l2, 0)),
'CategoryB': dict.fromkeys(l1, dict.fromkeys(l2, 0)),
'CategoryC': dict.fromkeys(l1, dict.fromkeys(l2, 0))}
d['CategoryA']['a']['c'] += 1