Hi, I’ve been trying to initialize a list of empty dictionaries and then loop through them to add some keys to each dictionary, unfortunately it seems that I get different output based on how I initialize my data structure. Here is a simplified example:
rows=[{},{},{}]
print(rows)
print('****************************************')
for i in range(len(rows)):
rows[i][i]=i+1
print(rows)
print('****************************************')
#OUTPUT:
#[{}, {}, {}]
#****************************************
#[{0: 1}, {}, {}]
#****************************************
#[{0: 1}, {1: 2}, {}]
#****************************************
#[{0: 1}, {1: 2}, {2: 3}]
#****************************************
The example above, where I’ve initialized my list of empty dictionaries with ‘[{ },{ },{ }]’ works as expected, I loop through my list and on each loop I add an item to the next dictionary. This is the intended behaviour. However, when I try to initialize with ‘[{ }]*3’ I get the following output:
#rows=[{},{},{}]
rows=[{}]*3
print(rows)
print('****************************************')
for i in range(len(rows)):
rows[i][i]=i+1
print(rows)
print('****************************************')
#OUTPUT:
#[{}, {}, {}]
#****************************************
#[{0: 1}, {0: 1}, {0: 1}]
#****************************************
#[{0: 1, 1: 2}, {0: 1, 1: 2}, {0: 1, 1: 2}]
#****************************************
#[{0: 1, 1: 2, 2: 3}, {0: 1, 1: 2, 2: 3}, {0: 1, 1: 2, 2: 3}]
#****************************************
Here whenever I try to add an item to one dictionary instead it gets added to all dictionaries in a list and I cant figure out why?
The only difference between the examples above is how I’ve initialized my data structure, but It shouldn’t matter since I’ve ended up with the same structure at the start of the loop.