Search for a partial string in an array of strings
<p>What is the Pythonic/quick way to search for a partial string in an array and then remove that string from the array?<br>
(I can do it with a simple loop and IF IN and rebuild two array in the loop, Asking if there is a Pythonic way/function to do this)</p>

<p>Example:  </p>

<pre><code>array = ['rule1','rule2','exception[type_a]','rule3','exception[type_b]']
res(,)=remove_exceptions(array,'exception')
print(res[0]) &gt;&gt;&gt; ['rule1','rule2','rule3']
print(res[1]) &gt;&gt;&gt; ['exception[type_a]','exception[type_b]']
</code></pre>

<p>For list-of-strings <code>array</code> and thing-to-exclude <code>target</code>:</p>

<p>List comprehensions work:</p>

<pre><code>result = [s for s in array if target not in s]
</code></pre>

<p>Or a generator comprehension for the same:</p>

<pre><code>result = (s for s in array if target not in s)
</code></pre>

<p>(<code>in</code> is effectively a contains operator, and <code>not in</code> is the inverse.)</p>

<p>Alternately, use the <code>filter()</code> <a href="https://docs.python.org/3.5/library/functions.html#filter" rel="nofollow">built-in</a> with a lambda:</p>

<pre><code>result = filter(lambda x: target not in x,
                array)
</code></pre>

<p>Either one returns a new object, rather than modifying your original list. The list comprehension returns a list, <code>filter()</code> returns a generator but you can wrap the call in <code>list()</code> if you need random access.</p>

python arrays python-3.x