Mar-24-2020, 04:58 PM
Hi there, I ran into a problem working with appending a dictionary to a list today. I eventually managed to figure out that appending a dictionary to a list without copying it is just appending a reference to that dictionary. Anyway I fixed it but I don't understand why:
In this code (the broken code), when I eventually print out my list I just get the same dictionary rather than different ones, which I understand happens because I referenced the dictionary rather than appending a copy to it each time.
This code over here is what I would think is correct:
Basically what I'm asking is in the first code. The whole idea of referencing breaks it BUT in the second code it works where I changed nothing but indentation and moving where I reset the dictionary, something which I'd think have no effect on referencing the dictionary, yet all of a sudden I get different copies of the same referenced dictionary unlike before where I just got a recurring copy.
In this code (the broken code), when I eventually print out my list I just get the same dictionary rather than different ones, which I understand happens because I referenced the dictionary rather than appending a copy to it each time.
labUpgradeDict = {} for row in labTableRows: cells = row.find_all("td") for cell in cells: labUpgradeDict[labTableHeaderSimplified[cells.index(cell)]] = cell.getText().replace('\n', '').strip() labUpgradeList.append(labUpgradeDict)Anyway I eventually changed my code to this, (changed the indent of the append for different reasons) and set the dictionary to being empty in the for loop.
for row in labTableRows: labUpgradeDict = {} cells = row.find_all("td") for cell in cells: labUpgradeDict[labTableHeaderSimplified[cells.index(cell)]] = cell.getText().replace('\n', '').strip() labUpgradeList.append(labUpgradeDict)Now this worked but I don't understand why because I'm still referencing the dictionary rather than appending a copy. Each entry in my final list had a different dictionary yet I'm just appending a reference to it so I thought that the final list would just contain a recurring dictionary. Yet it doesn't
This code over here is what I would think is correct:
for row in labTableRows: labUpgradeDict = {} cells = row.find_all("td") for cell in cells: labUpgradeDict[labTableHeaderSimplified[cells.index(cell)]] = cell.getText().replace('\n', '').strip() labUpgradeList.append(labUpgradeDict.copy())And it works in the same way as the code without the .copy().
Basically what I'm asking is in the first code. The whole idea of referencing breaks it BUT in the second code it works where I changed nothing but indentation and moving where I reset the dictionary, something which I'd think have no effect on referencing the dictionary, yet all of a sudden I get different copies of the same referenced dictionary unlike before where I just got a recurring copy.