Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the convinient way to evaluate multiple string equality in Python? [duplicate]

Tags:

python

I'm really stuck in evaluating Boolean expressions. See the code:

def f(A):
    if A=='a' or A=='b' or A=='c' ...:
        return True
    return False

Is there any convenient and elegant way to do this when A can equal to even more strings?

like image 753
YiFei Avatar asked Mar 08 '26 09:03

YiFei


1 Answers

If you do this check often and/or have a lot of possible values consider using a set. Lookup time for a set is O(1), lookup time for a list is O(n).

if A in {'a', 'b', 'c', ...}:
    # do something
like image 110
timgeb Avatar answered Mar 10 '26 23:03

timgeb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!