Remove a tuple from a list if the tuple contains a certain element
<p>I have a list of tuples (num, id):</p>

<pre><code>l = [(1000, 1), (2000, 2), (5000, 3)]
</code></pre>

<p>The second element of each tuple contains the identifier. Say that I want to remove the tuple with the id of <code>2</code>, how do I do that?</p>

<p>I.e. I want the new list to be: <code>l = [(1000,1), (5000, 3)]</code></p>

<p>I have tried <code>l.remove(2)</code> but it won't work =[</p>

<p>That's because the value <strong>2</strong> is not in the list.  Instead, something like the below: form a list of the second elements in your tuples, then remove the element at that position.</p>

<pre><code>del_pos = [x[1] for x in l].index(2)
l.pop(del_pos)
</code></pre>

<p>Note that this removes only the first such element.  If your instance is not unique, then use one of the other solutions.  I believe that this is faster, but handles only the single-appearance case.</p>

python