I have a dictionary of empty lists with all keys declared at the beginning:
>>> keys = ["k1", "k2", "k3"]
>>> d = dict.fromkeys(keys, [])
>>> d
{'k2': [], 'k3': [], 'k1': []}
When I try to add a coordinate pair (the list ["x1", "y1"]
) to one of the key's lists, it instead adds to all the keys' lists:
>>> d["k1"].append(["x1", "y1"])
>>> d
{'k1': [['x1', 'y1']], 'k2': [['x1', 'y1']], 'k3': [['x1', 'y1']]}
What I was looking for was:
>>> d
{'k1': [['x1', 'y1']], 'k3': [], 'k1': []}
How can I achieve this in Python 3?