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 use all() in python?

I want to check if all elements of a list are not present in a string.

ex :

    l = ["abc","ghi"]
    s1 = "xyzjkl"
    s2 = "abcdef"

So , when l is compared with s1 it should return True,
when l is compared with s2 it should return False.

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

This is what i tried :

    all(x for x in l if x not in s1) = True
    all(x for x in l if x not in s2) = True

I am getting True for both cases, But it should be false in second case.
Can someone please help, any solution will help, i just want to have it in a single line.

Thanks,

>Solution :

If the goal is:

Check that all elements of a list are not present in a string.

The pattern should be: all(s not in my_string for s in input_list)

l = ["abc","ghi"]
s1 = "xyzjkl"
s2 = "abcdef"

print(all(s not in s1 for s in l))  # True
print(all(s not in s2 for s in l))  # False
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