I have a nested list which has alway 5 values. The elements of the list can be different. I like to copy the list into two seperate lists and exchange in both new lists always two values.
The code is doing this but for the second loop the first list will be again overwritten. I don´t know why
Example:
test = [['1', '1', '1', '2024-03-03', "100"], ['1', '1', '2', '2024-05-03', "200"], ['1', '2', '2', '2024-05-03', "200"], ['1', '3', '3', '2024-01-03', "200"]]
print(test) # Okay
lst_01 = test.copy() # Okay
lst_02 = test.copy() # Okay
for item in lst_01:
item[3] = "2024-01-01"
item[4] = ""
print(lst_01) # Okay
print(lst_02) # Not Okay: Overwritten
for item in lst_02:
item[3] = "2024-12-31"
item[4] = ""
print(lst_01) # Mistake: First list again overwritten
print(lst_02) # Okay
sum = lst_01 + lst_02 # List lst_01 and lst_02 equal. This is not correct
print(sum)
Result of the code:
list(sum)
[['1', '1', '1', '2024-12-31', ''], ['1', '1', '2', '2024-12-31', ''], ['1', '2', '2', '2024-12-31', ''], ['1', '3', '3', '2024-12-31', ''], ['1', '1', '1', '2024-12-31', ''], ['1', '1', '2', '2024-12-31', ''], ['1', '2', '2', '2024-12-31', ''], ['1', '3', '3', '2024-12-31', '']]
The date is always "2024-12-31" for the first list "lst_01" Wher is my mistake?
Thanks for a feedback
I expect a new list base on the content of "test" list with changed values. The new list has exactly the doubled numbers of elements with new values