DORRITO
DORRITO

Reputation: 661

Multiple if conditions comparing to one

searched around a bit and can't find my exact problem the way I want to fix it.

What is the best way to shorten an if statement when having many conditions vs one check in python 3?

Example:

if "a" in word or "b" in word or "c" in word etc...:
    *do this*

What is the more correct/shorter way to do this? I do not want to loop through. (i.e. for i in whatever check if i is in whatever)

I've seen other examples like:

if {"a", "b", "c", etc...} in {word}:
    *do this*

or

if ("a" or "b" or "c" etc....) in word:
    *do this*

None of these work. Please help!

Upvotes: 2

Views: 68

Answers (1)

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23203

You may use any built-in function.

any(iterable)

Return True if any element of the iterable is true. If the iterable is empty, return False.

any(s in word for s in {'a', 'b', 'c'})

Upvotes: 4

Related Questions