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>You can use a list comprehension with a filter to achieve this.</p>

<pre><code>l = [(1000, 1), (2000, 2), (5000, 3)]
m = [(val, key) for (val, key) in l if key != 2]
</code></pre>

python