I am trying to create a dict of lists that can be appended to in a for loop. However, if I create a dict using fromkeys
, the list becomes a copy of a "pointer", not a new list. For example,
newdict = dict.fromkeys(range(10), [])
-- or --
newdict = dict.fromkeys(range(10), list())
both yield the same data structure, a dict with the SAME list as the value pair. So that when any key is updated e.g. - newdict[0].append(100)
, the corresponding output of print newdict
is:
{0: [100], 1: [100], 2: [100], 3: [100], 4: [100], 5: [100], 6: [100], 7: [100], 8: [100], 9: [100]}
Any thoughts on how to avoid this without having to iterate through in a for loop? Thanks in advance.