When I run the code below it removes deleted_partner from B. But as it removes it from B it also removes it from A. So that when I try to remove it from A the program crashes. What is the problem?
for deleted_partner in self.list_of_trading_partners:
B = A[:]
print("t", deleted_partner)
print(B[self.ID].list_of_trading_partners)
B[self.ID].list_of_trading_partners.remove(deleted_partner)
Round_neo_classic(B)
Round_costs(B)
if B[self.ID].final_good > reference_value:
print("d", deleted_partner)
print(A[self.ID].list_of_trading_partners)
A[self.ID].list_of_trading_partners.remove(deleted_partner)
Output:
('t', 1)
[1, 2, 3, 4]
('d', 1)
[2, 3, 4]
B=A[:]does copy only the list, but not it’s contents.B[self.ID]andA[self.ID]still reference the same object, onlyAandBare different.You might way to explicitly copy all the elements of the list too – copy.deepcopy can do this. But beware: deepcopy copies everything – it looks like you only want to copy the
list_of_trading_partners, so you should probably write a__deepcopy__method on whatever classA[self.ID]is that does just that.