Subtract two lists of tuples from each other
<p>I have the these two lists and I need to subtract one from the other but the regular "-" won't work, neither will .intersection or XOR (^). </p>

<pre><code>A = [(0, 1)]
B = [(0, 0), (0,1), (0, 2)]
</code></pre>

<p>Essentially what I want is:</p>

<pre><code>B - A = [(0, 0), (0, 2)]
</code></pre>

<p>If there are no duplicate tuples in <code>B</code> and <code>A</code>, might be better to keep them as sets, and use the <a href="https://docs.python.org/2/library/stdtypes.html#set.difference" rel="nofollow"><code>difference</code></a> of sets:</p>

<pre><code>A = [(0, 1)]
B = [(0, 0), (0,1), (0, 2)]
diff = set(B) - set(A) # or set(B).difference(A)
print(diff)
# {(0, 0), (0, 2)}
</code></pre>

<p>You could perform other operations like find the <a href="https://docs.python.org/2/library/stdtypes.html#set.intersection" rel="nofollow"><code>intersection</code></a> between both sets:</p>

<pre><code>&gt;&gt;&gt; set(B) &amp; set(A)
{(0, 1)}
</code></pre>

<p>Or even take their <a href="https://docs.python.org/2/library/stdtypes.html#set.symmetric_difference" rel="nofollow"><code>symmetric_difference</code></a>:</p>

<pre><code>&gt;&gt;&gt; set(B) ^ set(A)
{(0, 0), (0, 2)}
</code></pre>

python list tuples subtraction