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

Time:11-06

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/']

CodePudding user response:

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/
  • Related