Match similar item in list
<p>I have 2 lists of hostnames</p>

<pre><code>foo=['some-router-1', 'some-switch-1', 'some-switch-2']

bar=['some-router-1-lo','some-switch-1','some-switch-2-mgmt','some-switch-3-mgmt']
</code></pre>

<p>I would expect output to be like...</p>

<pre><code>out=['some-switch-3-mgmt']
</code></pre>

<p>I want to find entries in <code>bar</code> that are not in <code>foo</code>. However some names in <code>bar</code> have <code>"-mgmt"</code> or some other string appended that don't occur in <code>foo</code>. The length and number of dashes per list item vary greatly, so I'm not sure how successful using a regex would be. I'm new to programming, so please provide some explanation if possible.</p>

<p>There is some problems with the solutions provided by @Jim and @bbkglb when the elements are repeated in <strong><em>bar</em></strong>. Those solutions should be converted to <strong><em>sets</em></strong>. I tested the solutions and their response times:</p>

<pre><code>foo=['some-router-1', 'some-switch-1', 'some-switch-2']*1000
bar=['some-router-1-lo','some-switch-1','some-switch-2-mgmt','some-switch-3-mgmt']*10000
</code></pre>

<h3>Using <a href="http://www.python-course.eu/python3_lambda.php" rel="nofollow">filter - lambda</a>:</h3>

<pre><code>%timeit set(filter(lambda x: x if not any(x.startswith(f) for f in foo) else None, bar))
1 loop, best of 3: 7.65 s per loop
</code></pre>

<h3>Using <a href="https://docs.python.org/3/library/functions.html#all" rel="nofollow">all</a>:</h3>

<pre><code>%timeit set([i for i in bar if all(j not in i for j in foo)])
1 loop, best of 3: 7.97 s per loop
</code></pre>

<h3>Using <a href="https://docs.python.org/3/library/functions.html#any" rel="nofollow">any</a>:</h3>

<pre><code>%timeit set(b for b in bar if not any(b.startswith(f) for f in foo))
1 loop, best of 3: 7.97 s per loop
</code></pre>

python list