Python Forum
Checking 2 lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Checking 2 lists (/thread-20804.html)



Checking 2 lists - graham23s - Aug-30-2019

Hi Guys,

I have 2 lists, one is:

def allowed_email_links():
    """ this function keeps a list email structures we are allowed to click ... """
    return ["registration.activate",
            "/activate/"]
Which returns strings, these strings must be somewhere in a url for us to save them like:

            email_extract_urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', response[0][1].decode())

            if len(email_extract_urls) > 0:
                if allowed_email_links() in email_extract_urls:
                    email_container.append(email_extract_urls)
What i'm trying to do is, if any of the urls in the email_extract_urls list contains a string from the allowed_email_links() list then we will add it to the email_container

But the way i have it above it's looking for the string alone, rather than if it contains the string, is there a way to do this?

cheers guys

Graham


RE: Checking 2 lists - ichabod801 - Aug-31-2019

You generally do this with a loop:

email_container = []
allowed_links = allowed_email_links()
for extracted_url in email_extract_urls:
    for allowed_link in allowed_links:
        if allowed_link in extracted_url:
            email_container.append(extracted_url)
            break
You check each extracted url (outer for loop) against each allowed link (inner for loop). When you find a match, you include it and break to avoid including it more than once.


RE: Checking 2 lists - DeaD_EyE - Sep-01-2019

If the order is not important:

allowed_links = set(email_extract_urls) & set(allowed_email_links())
A set has only unique elements.