'Subtracting two lists with indices in Python [duplicate]
I have two lists, A1 and A2 with indices. I want to find out A1 - A2. The desired output is attached.
A1= [(0,0),(0,1),(0,2),(1,0),(1,1),(2,1),(2,0),(2,1),(2,2)]
A2= [(0,0),(0,1),(0,2)]
The desired output is
[(1,0),(1,1),(2,1),(2,0),(2,1),(2,2)]
Solution 1:[1]
You may try something like this
A3 = [i for i in A1 if not i in A2]
print(A3)
Solution 2:[2]
Just use list.remove
You can iterate in the list A2 and remove each element from A1:
A1= [(0,0),(0,1),(0,2),(1,0),(1,1),(2,1),(2,0),(2,1),(2,2)]
A2= [(0,0),(0,1),(0,2)]
for idx in A2:
A1.remove(idx)
print(A1)
and the output should be:
[(1, 0), (1, 1), (2, 1), (2, 0), (2, 1), (2, 2)]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Python learner |
| Solution 2 | Marco Colussi |
