Getting some confusing behaviour when running a for loop and removing entries from a list (cleaning out invalid urls):
urls = ['http://a.com/[email protected]','mailto:[email protected]', 'mailto:[email protected]', 'mailto:[email protected]', 'mailto:[email protected]']
for s in urls:
if '@' in s and '?' not in s:
urls.remove(s)
print(urls)
The output is:
['mailto:[email protected]', 'mailto:[email protected]']
It is consistently every other entry, so I'm assuming my understanding of python is not correct.
I looked into list comprehension with Python and ended up with:
urls = [s for s in urls if not ('?' not in s and '@' in s)]
This does what I want it to.
Is that the best way, can someone explain the behaviour, because I don't get it.
Thanks