I have two lists A and B. I want to delete the element in A from B. I present the current and expected output.
B=[4,5,1]
A=[1]
del B[A[0]]
print(B)
The current output is
[4,1]
The expected output is
[4,5]
I have two lists A and B. I want to delete the element in A from B. I present the current and expected output.
B=[4,5,1]
A=[1]
del B[A[0]]
print(B)
The current output is
[4,1]
The expected output is
[4,5]
As you mention the code, the code will delete an item with index A[0] which is 1. So you can visualize this del B[A[0]] = del B[1] = delete an element which has index 1 in B
Your final code should be:
You can set to do this
B=[4,5,1]
A=[1]
C = set(B) - set(A)
print(list(C))
in this code we are turning list to set and after subtract we will turn the result into list again
or use for loop
B=[4,5,1]
A=[1]
for item in A:
B.remove(item)
print(B)
here, you can use for loop, it will loop for every item in A and will remove it from B