Appending to dict of lists adds value to every key [duplicate]
问题 This question already has answers here : dict.fromkeys all point to same list (4 answers) Closed 5 years ago . 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']],