I am trying to create a list of dictionaries using the following python code, but I get a duplicate copy in list only
my code is :
def jsonHandler():
mqttDataToPublish = {}
jsonreaddata = []
jsoncommadnData = {}
for i in range(0,2,1):
for j in range(0,3,1):
print()
print()
print("befor request jsonreaddata",jsonreaddata)
print("first URL is ", j+1)
jsoncommadnData['command'] = j+1
jsoncommadnData['data'] = j+2
print("command and it url",jsoncommadnData)
print()
print()
print("after request jsonreaddata",jsonreaddata)
jsonreaddata.append(jsoncommadnData)
print()
print()
print("after append jsonreaddata",jsonreaddata)
def main():
jsonHandler()
if __name__ == "__main__":
main()
expected output is jsonreaddata = {[{'command': 1, 'data': 2}, {'command': 2, 'data': 3}, {'command': 3, 'data': 4}......
But got the following: all duplicate after append jsonreaddata [{'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}]
logs are:
python3 test.py
befor request jsonreaddata [] first URL is 1 command and it url {'command': 1, 'data': 2}
after request jsonreaddata []
after append jsonreaddata [{'command': 1, 'data': 2}]
befor request jsonreaddata [{'command': 1, 'data': 2}] first URL is 2 command and it url {'command': 2, 'data': 3}
after request jsonreaddata [{'command': 2, 'data': 3}]
after append jsonreaddata [{'command': 2, 'data': 3}, {'command': 2, 'data': 3}]
befor request jsonreaddata [{'command': 2, 'data': 3}, {'command': 2, 'data': 3}] first URL is 3 command and it url {'command': 3, 'data': 4}
after request jsonreaddata [{'command': 3, 'data': 4}, {'command': 3, 'data': 4}]
after append jsonreaddata [{'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}]
befor request jsonreaddata [{'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}] first URL is 1 command and it url {'command': 1, 'data': 2}
after request jsonreaddata [{'command': 1, 'data': 2}, {'command': 1, 'data': 2}, {'command': 1, 'data': 2}]
after append jsonreaddata [{'command': 1, 'data': 2}, {'command': 1, 'data': 2}, {'command': 1, 'data': 2}, {'command': 1, 'data': 2}]
befor request jsonreaddata [{'command': 1, 'data': 2}, {'command': 1, 'data': 2}, {'command': 1, 'data': 2}, {'command': 1, 'data': 2}] first URL is 2 command and it url {'command': 2, 'data': 3}
after request jsonreaddata [{'command': 2, 'data': 3}, {'command': 2, 'data': 3}, {'command': 2, 'data': 3}, {'command': 2, 'data': 3}]
after append jsonreaddata [{'command': 2, 'data': 3}, {'command': 2, 'data': 3}, {'command': 2, 'data': 3}, {'command': 2, 'data': 3}, {'command': 2, 'data': 3}]
befor request jsonreaddata [{'command': 2, 'data': 3}, {'command': 2, 'data': 3}, {'command': 2, 'data': 3}, {'command': 2, 'data': 3}, {'command': 2, 'data': 3}]
first URL is 3
command and it url {'command': 3, 'data': 4}
after request jsonreaddata [{'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}]
after append jsonreaddata [{'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}, {'command': 3, 'data': 4}]
can help what is missing or wrong in my code