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 could do this with a list comprehension and <code>all</code>:</p>

<pre><code>&gt;&gt;&gt; out = [i for i in bar if all(j not in i for j in foo)]    
&gt;&gt;&gt; out
['some-switch-3-mgmt']
</code></pre>

<p>Meaning, you select every element <code>i</code> in <code>bar</code> if, for every element <code>j</code> in <code>foo</code>, <code>j</code> is not contained in <code>i</code>.</p>

python list