I have a dictionary of empty lists with all the 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 (list ["x1", "y1"]) to one of the key lists, it instead adds all the key lists:
>>> d["k1"].append(["x1", "y1"])
>>> d
{'k1': [['x1', 'y1']], 'k2': [['x1', 'y1']], 'k3': [['x1', 'y1']]}
I was looking for:
>>> d
{'k1': [['x1', 'y1']], 'k3': [], 'k1': []}
How can I achieve this in Python 3?
source
share