I am trying to change only one out of three identical dictionary in my list using a for loop to slice out exactly the 2nd dictionary on my list. But somehow the values of all the 3 dictionaries in my list change as if i did not slice it correctly. What am I missing?
Code:
p1 = { 'fn': 'kan', 'ln': 'go', 'icolor': 'brown', 'age': 3.5, 'location': 'CA', } p_list = [] for i in range(0,3): p_list.append(p1) for p_info in p_list[1:2]: if p_info['fn'] == 'kan': p_info['fn'] = 'ad' p_info['ln'] = 'se' p_info['icolor'] = 'brown' p_info['age'] = 30 p_info['location'] = 'CN' print(p_list)
Actual Output:
[{'fn': 'ad', 'ln': 'se', 'icolor': 'brown', 'age': 30, 'location': 'CN'}, {'fn': 'ad', 'ln': 'se', 'icolor': 'brown', 'age': 30, 'location': 'CN'}, {'fn': 'ad', 'ln': 'se', 'icolor': 'brown', 'age': 30, 'location': 'CN'}]
Expected Output:
[{'fn': 'kan', 'ln': 'go', 'icolor': 'brown', 'age': 3.5, 'location': 'CA'}, {'fn': 'ad', 'ln': 'se', 'icolor': 'brown', 'age': 30, 'location': 'CN'}, {'fn': 'kan', 'ln': 'go', 'icolor': 'brown', 'age': 3.5, 'location': 'CA'}]
Advertisement
Answer
It is because all the three elements of the list are the same reference objects. Any changes made to one dictionary will change the elements in all the three dictionaries. Example:
If there’s a dictionary:
a={'x':1,'y'=2}
And there is a variable b which has exact value of a:b=a
Any changes made in a will effect b also and vice versa. But if you want to keep them independent, we need to create copies of it by copy()
method. If you type:
b=a.copy()
Now any changes made will not effect the other. The same goes with your code. We need to create three copies of your dictionary and you can do it by the command:
p_list = [p1.copy() for i in range(3)]
So your new code would be:
p1 = { 'fn': 'kan', 'ln': 'go', 'icolor': 'brown', 'age': 3.5, 'location': 'CA', } p_list = [p1.copy() for i in range(3)] for p_info in p_list[1:2]: if p_info['fn'] == 'kan': p_info['fn'] = 'ad' p_info['ln'] = 'se' p_info['icolor'] = 'brown' p_info['age'] = 30 p_info['location'] = 'CN' print(p_list)