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 correctly build an expression with comparison operators and brackets?

this not work well:

a = ['123','567','10', '223', '33']
for item in a:
    if ('5' or '1' or '2') in item:
        print(item)

I want to get any item in which there is at least one match with the numbers 1 or 2 or 5
My version is very cumbersome:

if '5' in item or '1' in item or '2' in item:

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

>Solution :

You can use any,

a = ['123','567','10', '223']
for item in a:
    if any(i in item for i in ('5','1','2')):
        print(item)
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