Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if a list contains a specific words which is part of another list

Hello I am new to python this may be something small but I am finding it difficult on how to get output for this

I have below list "sgList" which I want to check if it contains any of the specific words which is another list "checkforstrings" if yes then return true otherwise false

sgList   = ['Sensitive-ce-Public-TF_', 'cb-access', 'convc-service']

checkforstrings = ["-Public-", "-Private-", "-Protected-"]

in the above case this should return true coz ‘-Public-‘ is present in ‘Sensitive-ce-Public-TF_’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I know how to find a specific word in list but this may either in the starting/ending or middle so not sure how to check this
Any help would be greatly appreciated thank you

>Solution :

One option is to wrap a generator expression in any. The idea is we iterate over each string in checkforstrings and see if it’s in any string in sgList:

out = any(s in sg for s in checkforstrings for sg in sgList)

Output:

True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading