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>The biggest problem you have is that you don't save the return value. </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.']

stripped = list(urls) ## create a new copy, not necessary

for loc in locs:
    stripped = [url.replace(loc, '') for url in stripped]
</code></pre>

<p>After this, <code>stripped</code> is equal to</p>

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

<p><strong>EDIT</strong></p>

<p>Alternatively, without creating a new list, you can do</p>

<pre><code>for loc in locs:
    urls = [url.replace(loc, '') for url in urls]
</code></pre>

<p>After this, <code>urls</code> is equal to</p>

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

python list