It becomes less clear what you’re looking for when you start introducing multiple cases and potentially different list structures. Typically, any function is going to expect a defined structure. If you don’t know the structure (or you expect varying structures) you have explicitly handle those scenarios. We can modify the code to handle a single list or multiple sublists, but it’s much more beneficial, more stable, and usually easier, to ensure that you have a single structure that you can develop for.
So, again, the question becomes why are you changing from sublists to a single list? If your graph returns sublists but you need the output as a single list then we can just flatten after the fact. If your graph sometimes returns sublists and sometimes returns a single list I’d like to see why - this usually isn’t the case, and if it is, we can usually fix it.
We can still handle both cases by specifically building a check for structure and then handling either condition separately. We can use a singular function for both conditions, but we do have to treat those conditions as two separate possibilities. That means we now have 1) to develop a function for the single list condition (because that’s the most restrictive condition) and 2) include branches in our code that will handle whether we execute that function once for a single list or multiple times within a loop for a list of lists.
myList = IN[0]
searchTerm = "<EPItem ID=''>" #value you want to replace
n = 1 #starting increment
def ReplaceItemID(sublist,n): #checks each item in sublist and replaces term matches with the next incremental value
for i,item in enumerate(sublist): #enumerate so we have the value and the index
if item == searchTerm: #look for matches
newID = "'"+str(n)+"'" #create our new incremental ID
newTerm = searchTerm.replace("''",newID) #replace the original blank value with the new ID
sublist[i] = newTerm #replace the item at the matching index with the new value
n = n+1 #increment
return sublist
isSublist = isinstance(myList[0], list) #checks to see if the list contains sublists
if isSublist: #if sublists, search each sublist and replace
for sub in myList:
ReplaceItemID(sub,n)
n = n+1
else: #if no sublists, replace in initial list
ReplaceItemID(myList,n)
OUT = myList #return the updated list