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>You can do such operations by converting the lists to sets. Set difference:</p>

<pre><code>r = set(B)-set(A)
</code></pre>

<p>Convert to list if necessary: list(r)</p>

<p>Working on sets is efficient compared to running "in" operations on lists: <a href="http://stackoverflow.com/questions/25294897/why-is-converting-a-list-to-a-set-faster-than-using-just-list-to-compute-a-list">using lists vs sets for list differences</a></p>

python list tuples subtraction