Need help to place different items from sublists at a specific index of list with Python

Hi everyone,

I have a list with elements and i need to check in a group of sublists which elements are equal and place the different ones in the list before the first common item.

So far i’ve manage to make it work considering a sublist at a time, but i’m strugling to define all the sublists at once in a loop.

I apreciate any help.


SetDifferentElementsAtIndex.dyn (12.7 KB)

Hello @simoes.rja

try this function (recursive)

def seqatIndex(mainLst, lstB):
	if len(lstB) > 0:
		seq = lstB.pop(0)
		valueToAdd = seq.pop(0)
		for i , value in enumerate(mainLst):
			if mainLst[i:i+len(seq)] == seq:
				mainLst.insert(i , valueToAdd)
				return seqatIndex(mainLst, lstB)
	else:
		return mainLst
		
lsta = ["4MC","3MC","2MC","1MC"]
lstb = [["5MC","3MC","2MC","1MC"], ["6MC","1MC"]]

newLst = seqatIndex(lsta, lstb)
#print newLst

@c.poupin thanks for your reply

I tried your function but it didn’t work out…
However, i finally got it right by creating a function myself, it’s not fancy at all, but it seems to do what i need.

@simoes.rja

a small variant

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

def seqatIndex(mainLst, lstB):
    if len(lstB) > 0:
        seq = lstB.pop(0)
        valueToAdd = seq.pop(0)
        for i , value_ in enumerate(mainLst):
            if value_ == seq[0]:
                mainLst.insert(i , valueToAdd)
                return seqatIndex(mainLst, lstB)
    else:
        return mainLst
        
lsta = IN[0]
lstb = IN[1]

OUT = seqatIndex(lsta, lstb)
1 Like

@c.poupin this is a small example of the sublists i need to check. Some of them will add more than 1 item to the main list, but i’ll check your function and try to adapt it.

Thanks a lot!

1 Like