0

I have been trying to optimise this, but since each if has its own condition, i am not able to reduce its complexity

  for () in ():
      
        if ():
            if ():
               #Logical condition
               if ():
                #Logical condition
               else:
                #Logical condition
            
#another condition
                 if (): 
                  if (): 
                    if ():
                      #Logical condition
                      if index in ():
                        #Logical condition
Edo Akse
  • 4,051
  • 2
  • 10
  • 21
Pearl
  • 1
  • Please, take a while to read [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Corralien Jul 15 '21 at 12:53
  • Multiple `if` in a row should be combined with `&`. `if cond1: if cond2: if cond3` is the same as `if cond1 & cond2 & cond3:` – Corralien Jul 15 '21 at 12:56

1 Answers1

1

Logic operators are your friend. and, or, xor, etc

for () in ():
    if () and ():
        #Logical condition
        if ():
            #Logical condition
            pass
        else:
            #Logical condition
            pass

#another condition
    if () and () and () and index in ():
        #Logical condition
        pass
Edo Akse
  • 4,051
  • 2
  • 10
  • 21
  • Hello! Thank you for Answering. While adding the next condition, there are two logical conditions I need to add... just let me know how to add the second one as well. Thanks again! – Pearl Jul 15 '21 at 13:08
  • which *next condition* are you talking about? It's not exactly clear from how you describe it. That being said, if you have 2 conditions that both need to be met, you would use `if condition1 == True and condition2 == True` – Edo Akse Jul 15 '21 at 13:21
  • I tried your method but the execution time for 1000 data has increased from 65 seconds to 72 seconds which is undesired. Do let me know the alternatives – Pearl Jul 15 '21 at 13:35