cleaning urls and saving them to a txt file python3

That is because you removing items from the list while iterating over it, which is a bad thing to do, you could either create another list that has the new values and append to it, or modify the list in-place using indexing, you could also just use a list comprehension for this task:

content = [item if item.startswith(url_format) else re.sub(r'.*google', url_format, item) for item in content]

Or, using another list:

new_content = []

for item in content:
    if item.startswith(url_format):
        new_content.append(re.sub(r'.*google', url_format, item))

Or, modifying the list in-place, using indexing:

for i, item in enumerate(content):
    if not item.startswith(url_format):
        content[i] = re.sub(r'.*google', url_format, item)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top