Remove element from itertools.combinations while iterating?
<p>Given a list <code>l</code> and all combinations of the list elements is it possible to remove any combination containing <code>x</code> while iterating over all combinations, so that you never consider a combination containing <code>x</code> during the iteration after it is removed?</p>

<pre><code>for a, b in itertools.combinations(l, 2):
    if some_function(a,b):
        remove_any_tup_with_a_or_b(a, b)
</code></pre>

<p>My list <code>l</code> is pretty big so I don't want to keep the combinations in memory.</p>

<p>If you want to remove all tuples containing the number <code>x</code> from the list of combinations <code>itertools.combinations(l, 2)</code>, consider that you there is a one-to-one mapping (mathematically speaking) from the set <code>itertools.combinations([i for i in range(1,len(l)], 2)</code> to the <code>itertools.combinations(l, 2)</code> that <strong>don't</strong> contain the number <code>x</code>.</p>

<p><strong>Example:</strong></p>

<p>The set of all of combinations from <code>itertools.combinations([1,2,3,4], 2)</code> that don't contain the number 1 is given by <code>[(2, 3), (2, 4), (3, 4)]</code>. Notice that the number of elements in this list is equal to the number of elements of combinations in the list <code>itertools.combinations([1,2,3], 2)=[(1, 2), (1, 3), (2, 3)]</code>. </p>

<p>Since order doesn't matter in combinations, you can map 1 to 4 in <code>[(1, 2), (1, 3), (2, 3)]</code> to get <code>[(1, 2), (1, 3), (2, 3)]=[(4, 2), (4, 3), (2, 3)]=[(2, 4), (3, 4), (2, 3)]=[(2, 3), (2, 4), (3, 4)]</code>.</p>

python iterator combinations