Insert Sublist Item into Main List using Indexes

Hi,

I have got my main list and at certain indexes I want it to insert a single item from the sublist but all that happens is that the List.Insert inserts the whole sublist at the first index value.

Ideally at index 1 it would insert the first sublist item @L2 (03_Drainage), then at index 2 (03_Setting Out) and so on.

Is what I want possible with the list node or do I need to look at a Python solution?

1 Like

There’s a custom node for this (I think from Orchid but it just can’t remember.) Python would be pretty easy though.

I realized what I was asking the code block to do is pretty much impossible as each time an item is added the index values would need adjusted. Here is the Python Script I did should anyone else come across a similar issue.

Enable Python support and load DesignScript library

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

# The inputs to this node will be stored as a list in the IN variables.

oldList = IN[0]
elements = IN[1]
index = IN[2]
newList = []
	
for i in range(len(oldList)):
	for j in range(len(index)):
		if index[j] == i:
			newList.append(elements[j])
	newList.append(oldList[i])	

OUT = newList
1 Like

You’re exactly right. It’s a little more involved than just inserting items at the specified index.
Here’s the difference between inserting at the specified index (first python script) and inserting at the modified index (second python script).

dataEnteringNode = IN
originalList = IN[0]
newItems = IN[1]
indices = IN[2]
i = 0

for newItem,index in zip(newItems,indices):
	originalList.insert(index+i,newItem)
	i = i+1

OUT = originalList

The python code is the same except for the increment variable i to take into account the shift of elements with each insert.

2 Likes

I started out trying to use the original list and got totally lost with the changing index, so it’s great to have your example of how to use the original list.

Hi Boyts,

Which is the python node for nserting at the specified index ?

Regards

@enrique.sg It’s all explained in my post above.

1 Like