reduce the number of IF statements in Python

python optimize if statements
python refactor if statements
if statement replace python
how to avoid multiple if else in python
python how many elifs
python too many if statements
python reduce if
python if-elif alternative

I have written a function that is going to have up to 72 IF statements and i was hoping to write code that will be much shorter, but have no idea where to start

The function reads the self.timeselect variable when a radio button is selected and the result is saved to a text file called missing_time.txt. If the result is equal to 1 then save "0000" to the file, if the result is 2 save then 0020 to the text file etc. This can be for 72 possible combinations.

Is there a smarter way to simplify the function ?

def buttonaction():
    selectedchoice = ""            
    if self.timeselect.get() == 1:                
        selectedchoice = "0000"
        orig_stdout = sys.stdout
        f = open('missing_time.txt', 'w')
        sys.stdout = f
        print(selectedchoice)
        f.close()
    if self.timeselect.get() == 2:                
        selectedchoice = "0020"
        orig_stdout = sys.stdout
        f = open('missing_time.txt', 'w')
        sys.stdout = f
        print(selectedchoice)
        f.close()

self.timeselect = tkinter.IntVar()

self.Radio_1 = tkinter.Radiobutton(text="0000",variable = 
self.timeselect,indicator = 0 ,value=1)  
self.Radio_1.place(x=50,y=200)
self.Radio_2 = tkinter.Radiobutton(text="0020",variable = 
self.timeselect,indicator = 0 ,value=2)  
self.Radio_2.place(x=90,y=200)
choice_map = {
    1 : "0000",
    2 : "0020"
}

def buttonaction():
    selected = self.timeselect.get()        
    if 0 < selected < 73:  # This works as intended in Python
        selectedchoice = choice_map[selected]
        # Do you intend to append to file instead of replacing it? 
        # See text below.
        with open("missing_time.txt", 'w') as outfile:
            outfile.write(selectedchoice + "\n")
            print(selectedchoice)

Better yet, if there is a pattern that relates the value of self.timeselect.get() to the string that you write out, generate selectchoice directly from that pattern instead of using a dictionary to do the mapping.

Edit

I find it a bit odd that you are clearing the file "missing_time.txt" every time you call buttonaction. If your intention is to append to it, change the file mode accordingly.

Also, instead of opening and closing the file each time, you might just want to open it once and pass the handler to buttonaction or keep it as a global depending on how you use it.

Finally, if you do not intend to catch the KeyError from an invalid key, you can do what @Clifford suggests and use choice_map.get(selected, "some default value that does not have to be str").

Best Practices And Tips For Python Conditional Statements , For example, too many levels of indentations can easily make the code exceed the limit of words per line specified in PEP8. Let's take a look at  Reduce number of if statements on python. It works with all those if statements, but I was wondering whether it is possible to refactor a huge number of ifs into

All you need to do in this case is construct a string from the integer value self.timeselect.get().

selectedchoice = self.timeselect.get()
if 0 < selectedchoice < 73:
    orig_stdout = sys.stdout
    f = open('missing_time.txt', 'w')
    sys.stdout = f
    print( str(selectedchoice).zfill(4) )     # Convert choice to 
                                              # string with leading
                                              # zeros to 4 charaters
    f.close()

Further in the interests of simplification, redirecting stdout and restoring it is a cumbersome method of outputting to a file. Instead, you can write directly to the file:

    with open('missing_time.txt', 'w') as f:
        f.write(selectedchoice + "\n")

Note that because we use the with context manager here, f is automatically closed when we leave this context so there is no need to call f.close(). Ultimately you end up with:

selectedchoice = self.timeselect.get()
if 0 < selectedchoice < 73:
    with open('missing_time.txt', 'w') as f:
        f.write( str(selectedchoice).zfill(4) + "\n" )

Even if you did use the conditionals each one differs only in the first line, so only that part need be conditional and the remainder of the content performed after the conditionals. Moreover all conditionals are mutually exclusive so you can use else-if:

    if self.timeselect.get() == 1:                
        selectedchoice = "0000"

    elif self.timeselect.get() == 2:                
        selectedchoice = "0020"

    ...

    if 0 < selectedchoice < 73:
        with open('missing_time.txt', 'w') as f:
            f.write(selectedchoice + "\n")

In circumstances where there is no direct arithmetic relationship between selectchoice and the required string, or the available choices are perhaps not contiguous, it is possible to implement a switch using a dictionary:

choiceToString = {
    1:  "0001",
    2:  "0002",
    ...
    72: "0072",
}

selectedchoice = choiceToString.get( self.timeselect.get(), "Invalid Choice")

if selectedchoice != "Invalid Choice":
    with open('missing_time.txt', 'w') as f:
        f.write(selectedchoice + "\n")

Changing large number of if-elif-else statements to use underlying , Your initial function executes one specific step for one specific type, both of them being provided as argument. In the second case you iterate  Python's nested if statements: if code inside another if statement. A nested if statement is an if clause placed inside an if or else code block. They make checking complex Python conditions and scenarios possible. Compare values with Python's if statements: equals, not equals, bigger and smaller than

Since there is no switch statement in Python, you can't really reduce the number of if statements. But I see 2 two way to optimize and reduce your code length.

First, you can use some

if condition:

elif condition:

instead of

if condition:

if condition:

since you can't have self.timeselect.get() evaluated to more than one int. Secondly you can wrap all the code that doesn't vary in a function. You can get rid of selectedchoice and put

orig_stdout = sys.stdout
f = open('missing_time.txt', 'w')
sys.stdout = f
print(selectedchoice)
f.close()

in a function writeToFile(selectedOption)

3.1. If Statements, Since it is dedented, it is not a part of the if-else statement: Since its amount of a single test may only reduce the possibilities, but further tests can reduce the  In Python, the body of the if statement is indicated by the indentation. Body starts with an indentation and the first unindented line marks the end. Python interprets non-zero values as True. None and 0 are interpreted as False. Python if Statement Flowchart. Example: Python if Statement. # If the number is positive, we print an appropriate

I'm assuming that the values are arbitrary and there's no defined pattern. I also see that the only thing that changes in your code is the selectedChoice variable. You can use a Dictionary in such cases. A dictionary's elements are key/value pairs so you can reference the key and get the value.

dictionary = {
    1:"0000",
    2:"0020",
    3:"0300",
    4:"4000"
}

def buttonAction():
    selectedChoice = dictionary[self.timeselect.get()]
    if 0<selectedChoice<=72:
        f=open('missing_time.txt','w')
        f.write(selectedChoice+" ")
        f.close()
        print(choice)

Python tips: don't be too concise, But not always; eventually you'll reach a point where you're reducing for let's look at a simple example: a function which returns numbers from def fib(n): if n == 0: return 0 elif n == 1: return 1 else: return fib(n-1) + fib(n-2). 3.1.1. Simple Conditions¶. The statements introduced in this chapter will involve tests or conditions.More syntax for conditions will be introduced later, but for now consider simple arithmetic comparisons that directly translate from math into Python.

Using the if, else statement with reduce() and lambda(), Python Functional Python The Lambda Lambada Lambda 2 I think I got the beginning portion with reduce and lambda right. I think you need to use the if statement here but I'm not sure how to incorporate that I used that if else statement for calculating total weight of my number with my second count list. An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at most only one else statement following if.

Python Programming/Conditional Statements, number') else: if guess < answer: print ('Little higher') elif guess > answer: print ('Little lower') else: # guess == answer print ('MINDREADER!!!') break. even.py. reduce() in Python. The reduce(fun,seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.This function is defined in “functools” module. Working : At first step, first two elements of sequence are picked and the result is obtained.

The art of avoiding nested code - The Python corner, (using List Comprehension, Map Filter & Reduce functions and the ternary conditional operator). Guys, the Python corner has a new home and it's a great place,  A Python if else statement takes action irrespective of what the value of the expression is. If the result is True, then the code block following the expression would run. Otherwise, the code indented under the else clause would execute. Given below is the syntax of Python if Else statement.

Comments
  • Very detailed explanation on the reason for the changes. +1. Just a few minor comments. We can write out to file directly using the file object f using f.write(selectedchoice + "\n") and Python allows the condition to be expressed as 0 < selectedchoice < 73. But overall an in depth exposition of the thought process behind the refactor.
  • @lightalchemist : As you can see I am not a Python programmer; just a programmer! (C++ on embedded systems primarily). I'd apply your suggestions, but am hardly qualified to do so. I answered most of this by googling Python syntax! Feel free to edit for me.