I am trying to filter some url from my fetch list by using for loop and if condition in python

Question:

I am trying to filter some url from my fetch list but its not applying the filter it is giving me the same output from fetchurl in weblinks
Here is my code

weblinks =[]
fetchurl = ['https://www.youtube.com/watch?v=___nkvwpnAg&t=1218', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/'] 

 filterlist = ["youtube.com","twitter.com","facebook.com","google.com","tiktok.com"]
        for ur in fetchurl:
                for i in range(len(filterlist)):
                        if filterlist[i] not in ur:
                                weblinks.append(ur)
        print("weblinks url working", weblinks)

output

weblinks url working ['https://www.youtube.com/watch?v=___nkvwpnAg&t=1218', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/']
Asked By: Info Rewind

||

Answers:

The error was that you were adding an element if any of the values from the filterlist did not match, but you should if all the values did not match. If at least one match is found, then the inner loop will exit early and the element will not be included in the resulting list. If there is no match, the loop will run in its entirety and else: will work, then the element will be included in the final list.

weblinks = []
fetchurl = ['https://www.youtube.com/watch?v=___nkvwpnAg&t=1218',
            'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
            'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
            'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
            'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
            'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
            'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/']

filterlist = ["youtube.com", "twitter.com", "facebook.com", "google.com", "tiktok.com"]

for ur in fetchurl:
    for i in filterlist:
        if i in ur:
            break
    else:
        weblinks.append(ur)
print("weblinks url working", *weblinks,sep='n')
weblinks url working
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/
Answered By: Алексей Р
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.