Filling a python dictionary in for loop returns same values

后端 未结 2 1091
长发绾君心
长发绾君心 2020-12-11 08:44

For the needs of the project im iterating over some data and adding needed values to premade dictionary.

here is striped down example of code which represents my que

2条回答
  •  庸人自扰
    2020-12-11 09:22

    You are modifying and inserting the same dictionary to your list three times. Create a new dictionary for each iteration of the loop:

    for i in self.records:
        dic = { 'key1': i[0], 'key2': i[1], 'key3': i[2] }
        parsed.append(dic)
    

提交回复
热议问题