0

Here is the code I'm working with. Language is python.

For a class, we are asked to write a function for basic boolean operations. This one is for subtracting two lists.

def bool_min(A,B):

subtracted = A
removal_list = []
for x in A:
    for y in B:
        if x == y:
            removal_list.append(x)
print(f'Removal list is {removal_list}')
for z in removal_list:
    subtracted.remove(z)
return subtracted

When the code reaches subtracted.remove(z), it removes the value from the 'subtracted' list as expected. However, for some reason it also removes the same value from the list 'A', which is only used to input into the function.

Edit: changed out subtracted = A for the following, it fixed the issue:

subtracted = [] for w in A: subtracted.append(w)

However, I want to know what the difference between these two versions is, since subtracted = A seems to be a simple copy-paste into another variable. How are they linked through this?

  • Does this answer your question? [How do I clone a list so that it doesn't change unexpectedly after assignment?](https://stackoverflow.com/questions/2612802/how-do-i-clone-a-list-so-that-it-doesnt-change-unexpectedly-after-assignment) – Arifa Chan Sep 30 '22 at 23:58

0 Answers0