Home > Blockchain >  How to elegantly handle multiple .find() checks in an if check elegantly?
How to elegantly handle multiple .find() checks in an if check elegantly?

Time:10-31

So right now I have a line in my code with multiple value checks that gets extremely bulky. How would I declutter it?

if string.find(a) != -1 and string.find(b) != -1 string.find(b) != -1 and string.find(c)==-1 and string.find(d)==-1 string not in list:

I tried to handle everything in a for loop, which turned out to be quite the hassle to fulfill many conditions, which had me return to the long row above.

CodePudding user response:

An example using map and all

a = "abc 123 321"
i = ["abc", "123", "321"]

if all(map(lambda f: f in a, i)):
    print("All elements of i are in a")

CodePudding user response:

Just nest your ifs for better readability. Time complexity will not be a problem since you are just checking condition:

if string.find(a) != -1 and string.find(b) != -1:
    if string.find(b) != -1 and string.find(c)==-1 and string.find(d)==-1:
        if string not in lst:
            #your code here
  • Related