Python Remove List of Strings from List of Strings
<p>I'm trying to remove several strings from a list of URLs. I have more than 300k URLs, and I'm trying to find which are variations of the original. Here's a toy example that I've been working with.</p>

<pre><code>URLs = ['example.com/page.html',
        'www.example.com/in/page.html',
        'example.com/ca/fr/page.html',
        'm.example.com/de/page.html',
        'example.com/fr/page.html']

locs = ['/in', '/ca', '/de', '/fr', 'm.', 'www.']
</code></pre>

<p>What I'd like to end up with is a list of the pages without the language or locations:</p>

<pre><code>desired_output = ['example.com/page.html',
                  'example.com/page.html',
                  'example.com/page.html',
                  'example.com/page.html',
                  'example.com/page.html']
</code></pre>

<p>I've tried list comprehension and nested for loops, nothing has worked yet. Can anyone help?</p>

<pre><code># doesn't remove anything
for item in URLs:
    for string in locs:
        re.sub(string, '', item)

# doesn't remove anything
for item in URLs:
    for string in locs:
        item.strip(string)

# only removes the last string in locs
clean = []
for item in URLs:
    for string in locs:
        new = item.replace(string, '')
    clean.append(new)
</code></pre>

<p>You have to assign the result of <code>replace</code> to <code>item</code> again:</p>

<pre><code>clean = []
for item in URLs:
    for loc in locs:
        item = item.replace(loc, '')
    clean.append(item)
</code></pre>

<p>or in short:</p>

<pre><code>clean = [
    reduce(lambda item,loc: item.replace(loc,''), [item]+locs)
    for item in URLs
]
</code></pre>

python list