Home > front end >  How to check if a list contains a specific words which is part of another list
How to check if a list contains a specific words which is part of another list

Time:05-05

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_'

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

CodePudding user response:

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

CodePudding user response:

You can use the operator in.

If '-Public-' in 'Sensitive-ce-Public-TF'

Would return true.

  • Related