Is there any way to condense my code so it does not take up so many lines? I'm writing my program in python and have it working properly, but it's over 500 lines of code. I think there is a way I can condense my code, but I'm not sure how I should do it
Here's an example of some of my code:
def click():
    if clicked1.get() == &quot;app1&quot;:
        os.startfile(r&quot;C:\Users\red\Desktop\app1&quot;)
        i = 0
        while i &lt; 20:
            try:
                app.connect(title_re=&quot;.*app1&quot;)
                break
            except pywinauto.findwindows.ElementNotFoundError:
                time.sleep(1)
                i += 1
        app1 = app.top_window()
        app1.move_window(x1, y1)
        time.sleep(.5)
        app1.maximize()
    if clicked1.get() == &quot;app2&quot;:
        os.startfile(r&quot;C:\Program Files (x86)\app2&quot;)
        i = 0
        while i &lt; 20:
            try:
                app.connect(title_re=&quot;.*app2&quot;)
                break
            except pywinauto.findwindows.ElementNotFoundError:
                time.sleep(1)
                i += 1
        app1 = app.top_window()
        app1.move_window(x1, y1)
        time.sleep(.5)
        app1.maximize()

I basically have about 20 of those if statements for each different
application and for each different clicked (1-4). Is there any way I can shrink my code so it doesn't take up so many lines? Perhaps using variables?
When refactoring code that is not DRY then try to identify the parts that actually do change and put those parts into some kind of data structure like a list or dictionary. Then wrap everything into a function.
apps = {'app1': r&quot;C:\Users\red\Desktop\app1&quot;,
        'app2': r&quot;C:\Program Files (x86)\app2&quot;,
        ...
       }
       
selected_app = clicked1.get()

def click():
    os.startfile(apps.get(selected_app))
    i = 0
    while i &lt; 20:
        try:
            app.connect(title_re=&quot;.*&quot; + selected_app)
            break
        except pywinauto.findwindows.ElementNotFoundError:
            time.sleep(1)
            i += 1
    app1 = app.top_window()
    app1.move_window(x1, y1)
    time.sleep(.5)
    app1.maximize()