Numbering String Values based on rule

    result = []
    for index, item in enumerate(nested_list, start=1):
        if isinstance(item, list):
            if any(isinstance(subitem, list) for subitem in item):
                sublist_result, last_sd_value = convert_and_number_lk(item, index, last_sd_value)
                result.append(sublist_result)
            else:
                sublist_result = []
                for subitem in item:
                    if 'LK' in str(subitem):
                        if grandparent_index == 1:
                            sublist_result.append(grandparent_index)
                        else:
                            sublist_result.append(last_sd_value + 1)
                    elif '(SD)' in str(subitem):
                        sublist_result.append(grandparent_index + 1)
                        last_sd_value = grandparent_index + 1
                    else:
                        sublist_result.append('N/A')
                result.append(sublist_result)
        else:
            result.append('N/A' if isinstance(item, str) else item)
    return result, last_sd_value

IN = IN[0]  # The output from your previous code

OUT, _ = convert_and_number_lk(IN)

I am trying to number strings ‘LK’ and ‘(SD)’ within grandparent lists based on a certain rule. It does what its suppose to do for grandparent lists with ‘LK’ and ‘(SD)’ strings but then returns the incorrect value for for the first grandparent list with ‘(SD)’ and no ‘LK’ strings. It does continue to increment the values in the next grandparent list with the same condition. The input has been set up such that grandparent lists with ‘(SD)’ and no ‘LK’ strings will always come after the grandparent lists with (SD) and (LK) strings. Not sure how to fix the problem. Would love the help.

Hi! Couldn’t fully understand what your inputs are neither what you are trying to achieve.
You explained it well, but it’s hard, at least for me, to figure it out without seing an example of it - if you could share it, it would be helpfull.

Anyway, it sounds like it might be a problem with the way the variables are defined. Specifically, it appears to be a problem with a variable not being reset for each grandparent list as you said. If the value of it is still in memory, you might need to set it manually to 0 or 1 before the if/else statements.

Also, I couldn’t find where the var “grandparent_index” is defined, so this might also be a problem.

Don’t know if this was of any help, but we can still figure it out by testing it with different inputs.

1 Like

Apologies, A line got cut out. Thank you for reviewing the code. Please see the complete code below,


def convert_and_number_lk(nested_list, grandparent_index=1, last_sd_value=1):
    result = []
    for index, item in enumerate(nested_list, start=1):
        if isinstance(item, list):
            if any(isinstance(subitem, list) for subitem in item):
                sublist_result, last_sd_value = convert_and_number_lk(item, index, last_sd_value)
                result.append(sublist_result)
            else:
                sublist_result = []
                for subitem in item:
                    if 'LK' in str(subitem):
                        if grandparent_index == 1:
                            sublist_result.append(grandparent_index)
                        else:
                            sublist_result.append(last_sd_value + 1)
                    elif '(SD)' in str(subitem):
                        sublist_result.append(grandparent_index + 1)
                        last_sd_value = grandparent_index + 1
                    else:
                        sublist_result.append('N/A')
                result.append(sublist_result)
        else:
            result.append('N/A' if isinstance(item, str) else item)
    return result, last_sd_value

IN = IN[0]  # The output from your previous code

OUT, _ = convert_and_number_lk(IN)

Here is what I am trying to do:


The first part searches for strings ‘LK’ and (SD). It then starts the numbering from the first string with LK in it in the first grandparent list. It applies the same value to all the LK strings in the same grandparent list. Then all the strings (SD) in the same grandparent list will get the next number in the sequence. If there is another grandparent list after the first one it applies a slightly different logic where strings LK will get the next number in sequence after (SD) in grandparent list 1. The code is suppose to ignore the fact that the grandparent list with only (SD) strings is part of another sublist and count it in sequence.


The problem is when there is a grandparent list which has (SD) but no LK strings it applies the same value of the (SD) string from the previous grandparent list.


However the code does maintain the value throughout the grandparent list and does increment if there is a second grandparent list with only (SD) strings. All other strings that are not (SD) or LK get the value N/A.

Hope this helps. Thanks again!

Maybe have an example of what you have to start and what you want the outcome to be.

Also have an example of the start and the actual outcome you’re getting.

It sounds like you want a loop and maybe some if statements but i’m struggling to understand exactly what you expect it to do.

Sorry this one is taking me a second to wrap my head around as well. I updated the diagrams to show what I am trying to achieve. Please let me know if it helps.

So if you find a number 1 you want to replace it with “LK” and if you find 2 you want to replace it with “SD” ?

What is the relevance of the grandparent lists?

I can’t work out exactly what you want… But maybe loop through list 1 and add the items… Then append a count so list two gets a bigger number to add?

When sharing code on the forum please use the ‘pre formatted text’ option, otherwise important information such as indents and the like can be lost. You can use the button shown in the screenshot below, or wrap the code in triple carrot characters (`).

When the code finds strings LK in the first grandparent list it will all of them the value 1 and all the strings (SD) will receive the value 2. Then if the next grandparent list contains both LK and (SD) the code will number all the strings LK the value 3 and all the (SD) strings in that grandparent list the value 4. If the next grandaparent string calculated only has (SD) strings then the code needs to assign it the value. Right now the way my code is structured one sublist contains all the grandparent lists with both LK and (SD) and the next sublist contains all the grandparent lists that only have (SD) strings. because both are coming in from different data sets. Here is a badly stitched image of the complete example list.

So my previous suggestion should work then…
Start a count at zero and loop…
If you hit LK add 1
If you hit SK add 2

When the first list is done add 1 to the count.

So the last part is where I seem to be going wrong. I have tried applying the function ‘last_sd_value + 1’ to the grandparent list with only (SD) but the output doesn’t work because for some reason it applies the function to the strings individually which results in incremental values within the grandparent list. They should all have the same value. Let me know if I have lost you here.

Change the level where the count is?

Any chance you could show me using my code as an example. Seem to be lost here.

No idea if I understood you right… and I’ve not nested as much as you… But the general principal should be the same

That’s close but the numbering in your code seems to be based on the parent list. The values of strings LK and (SD) should remain the same within the grandaparent list. That would mean all the strings in your list should be 1 and 2. Strings LK and (SD) in the second grandparent list will get the values 3 and 4 or if the second grandparent list only has (SD) strings then those strings should return the value 3. Sorry I know its a Sunday lol.

So you’re saying… You’re looking for LK first… and that adds one.
if there is an SD too then add another one… otherwise just stay at that number?

In that case… loop and add one if there’s LK and another if SK… then that adds to the count.

Oh and to make matters worse the grandparent list with only (SD) is part of another input so it comes in as another sublist within the grandparent list. I do need to maintain that structure for applying values to parameters but the code I pasted seems to bypass that.

1 Like

Well if there is no LK then (SD) gets the next number after the one that was applied to (SD) in the list before it.

haha! Sorry… I am so confused.

Maybe someone else can decipher this :smiley:

1 Like

lol I feel you man. You were pretty close:

The code should return values like this:

Below is a case with mulitple grandparent lists with LK and (SD) strings.
If we were to zoom out, the list structure looks like this:

I still don’t know if I figured it out right, but I believe having a counter like the exemple above (a global var, for example) would be a great approach.

This way, you could increment it by one if there is at a least one “LK” in the grandparent list and increase it again by one if there are any “SD” without possibly losing track of it. Even if you need to reset it after certain interaction, you would be able to do so easily.

You could argue that this way you’d have the wrong number replacing the “LK” values but there are many ways you could contour this. One of them would be appending the “LK” items before increasing the counter for the “SD” items and, then, appending/replacing the “SD” values for the new counter value.

But there are many other options: you could recreate the output list entirely or even check if there are any “LK” and any “SD” in each interaction. Some of theses aren’t good practices but you should choose what makes you more comfortable.

Anyway, even if it’s not the solution, I believe that this is one of those cases in which trying to simplify the code could help a lot. Not saying that it’s easy, but sometimes it can save a lot of time and future headaches.

I’m rooting for your success in this one!