Using list comprehension to keep items not in second list
<p>I am trying to use list comprehension to remove a number of items from a list by just keeping those not specified.</p>

<p>For example if I have 2 lists <code>a = [1,3,5,7,10]</code> and <code>b = [2,4]</code> I want to keep all items from <code>a</code> that are not at an index corresponding to a number in <code>b</code>.</p>

<p>Now, I tried to use <code>y = [a[x] for x not in b]</code> but this produces a SyntaxError.</p>

<p><code>y = [a[x] for x in b]</code> works fine and keeps just exact the elements that i want removed.</p>

<p>So how do I achieve this? And on a side note, is this a good way to do it or should I use <code>del</code>?</p>

<p>Guess you're looking for somthing like :  </p>

<pre><code>[ x  for x  in a if a.index(x) not in b  ] 
</code></pre>

<p>Or, using filter:</p>

<pre><code>filter(lambda x : a.index(x) not in b , a)
</code></pre>

python list list-comprehension not-operator