Getting some confusing behaviour when running a for loop and removing entries from a list (cleaning out invalid urls):
urls = ['http://a.com/?mail=a@b.com','mailto:a@a.com', 'mailto:a@b.com', 'mailto:a@c.com', 'mailto:a@d.com']
for s in urls:
if '@' in s and '?' not in s:
urls.remove(s)
print(urls)
The output is:
['mailto:a@b.com', 'mailto:a@d.com']
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