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'}]