good day
When adding a dictionary array inside a list I am not able to modify one of the dictionary items, instead all the dictionary items are modified...
#create a dictionary
dcirc={'mode':1,'alarm':4,'step':7}
#append the same dictionary in a list for 3 times
lcirc=[]
for x in range(3):
lcirc.append(dcirc)
#modify item 'alarm' from list array 2
lcirc[2]['alarm']=99
Result: (all the 'alarm' item changes)
[{'mode': 1, 'alarm': 99, 'step': 7}, {'mode': 1, 'alarm': 99, 'step': 7}, {'mode': 1, 'alarm': 99, 'step': 7}]
The only way I can get it to work is If I create the dictionary directly in the append instruction (instead of using the dictionary variable).
#append the dictionary in a list
lcirc=[]
for x in range(3):
lcirc.append({'mode':1,'alarm':4,'step':7}) #<--- NOT using dictionary variable
#modify item 'alarm' from list array 2
lcirc[2]['alarm']=99
Result: **It works
[{'mode': 1, 'alarm': 4, 'step': 7}, {'mode': 1, 'alarm': 4, 'step': 7}, {'mode': 1, 'alarm': 99, 'step': 7}]
I am just wondering what I am doing wrong when using the dictionary variable that causes problems when trying to modify one item.. any clues?