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>You may achieve it by using <code>filter</code> as:</p>

<pre><code>&gt;&gt;&gt; filter(lambda x: x if not any(x.startswith(f) for f in foo) else None, bar)
['some-switch-3-mgmt']
</code></pre>

<p>I am using <code>startswith</code> to check whether any element of <code>bar</code> starts with any element of <code>foo</code></p>

python list