Remove numbers from list which contains some particular numbers in python
<p>Given List:</p>

<pre><code>l = [1,32,523,336,13525]
</code></pre>

<p>I am having a number 23 as an output of some particular function.</p>

<p>Now, </p>

<p>I want to remove all the numbers from list which contains either 2 or 3 or both 2 and 3.</p>

<pre><code>Output should be:[1]
</code></pre>

<p>I want to write some cool one liner code.</p>

<p>Please help!</p>

<p>My approach was :</p>

<p>1.) Convert list of int into list of string.</p>

<p>2.) then use for loop to check for either character 2 or character 3 like this:
<code>A=[x for x in l if "2" not in x]</code> (not sure for how to include 3 also in this line)</p>

<p>3.) Convert A list into integer list using :</p>

<p><code>B= [int(numeric_string) for numeric_string in A]</code></p>

<p>This process is quiet tedious as it involves conversion to string of the number 23 as well as all the numbers of list.I want to do in integer list straight away.</p>

<p>You're looking for the <code>filter</code> function.  Say you have a list of ints and you want the odd ones only:</p>

<pre><code>def is_odd(val):
   return val % 2 == 1

filter(is_odd, range(100))
</code></pre>

<p>or a list comprehension</p>

<pre><code>[x for x in range(100) if x % 2 == 1]
</code></pre>

<p>The list comprehension in particular is perfectly Pythonic.  All you need now is a function that returns a boolean for your particular needs.</p>

python list python-2.7 python-3.x filter