Loads are duplicated in second run and keeps adding in N-times run (API/ Dynamo)

Hi there,
I’m a little late to the party, and forgive me if I’ll sound harsh, but I’m on a mission of teaching a better python style coding to as many people I can :smile:

This will hopefully be useful to prevent propagating code ugliness via copy-pasting…

import clr
# We don't need Protogeometry library for this, script, removed
# With RSA 2023 we can point directly to the app folder
clr.AddReferenceToFileAndPath(r"C:\Program Files\Autodesk\Robot Structural Analysis Professional 2023\Exe\Interop.RobotOM.dll")
# import * is a bad practice in python world, better to explicitly import what whe need
from RobotOM import RobotApplicationClass
# no need to import System.Object, removed

# python code style uses snake_case for variables names, and C# variables should begin with a lowercase letter...
# I know it's a matter of styling and personal preference, but sticking to best practices helps you and other to quickly identify what are you talking about...
# That said I'm keeping them as they are 
CombCaseNumber = IN[0]
CombCaseName = IN[1]
CCombNumber = IN[2]
CFactors = IN[3]
IRCBT = IN[4]
IRCN = IN[5]
IRCAT = IN[6]

application = RobotApplicationClass()
# you use only loads, so you can keep just it
loads = application.Project.Structure.Cases

# here you use a loop on range(len(list)) to get the index and then retrieve the element;
# you can save this by using enumerate(list) that returs both the index and the element
# for k, number in enumerate(CombCaseNumber):
# and then replace CombCaseNumber[k] with number,
# or even use zip() to get all the corresponding items from all the lists
# (maybe this is more unreadable than before, as there are many lists involved):
for num, name, caseNumbers, caseFactors, combType, combNature, comAnalyzeType in zip(CombCaseNumber, CombCaseName, CCombNumber, CFactors, IRCBT, IRCN, IRCAT):
    # no need to use parenthesis in with if
    if loads.Exist(num):
        # in my test, selectioncases is useless because we already have the number to delete;
        # and since project.Structure.Cases is the same as loads, you can just call
        loads.Delete(num)
    # the rest of the code is duplicated, since we already know that the load doesn't exist anymore and we need to create the new one in any case..
    # again we directly use loads and take advantage of the zip()ed objects
    caseComb = loads.CreateCombination(num, name, combType, combNature, comAnalyzeType)
    # no Idea why there is a IRobotCaseFactorMngr hanging around here, removed
    # we can save some CPU by storing the caseFactors outside the loop
    caseManage = caseComb.Casefactors
    # another loop that can be simplified with zip
    for cNum, cFactor in zip(caseNumbers, caseFactors):
        caseManage.New(cNum, cFactor)

Out = CombCaseNumber