Place Item at index

dynamo

#1

Hello guys,

I have a List and want to insert multiple values on multiple indexes. It seems so easy but I dont get how to do it. And didnt find it in the forum.
Is there no node like List.PlaceItematindex?
With List.Insert it everytime takes the whole list. But I want to have the single values.


First time it should take first value, secont time second value and so on. How to do it?

Any help would be appreciated! Thanks a lot!


#2

I use this one, I’m sure the forum will provide you will a million examples :slight_smile:


#3

Part of the question becomes when should the next item index be based on? The original list or the updated list after the first item.

So after you insert 1 at index 1, should 2 be inserted into the current index 3 or the original index 3 (now at index 4).


#4

Thats great thanks! which package is it?


#5

I dont get it 100% how you mean this. But the outcome should be exacly like the post from @Mark.Ackerley. :slight_smile:


#6

RIE is the package in this case…As a rule of thumb, if the node has a funny prefix it’s usually the package name…


#7

Sorry, what I mean is that there are two ways to interpret inserting using list of items and list of indices.

Let’s say you wanted the value 3 to be inserted into the list in this example. Since it is the third item to be inserted, the items at index 9 has changed. Instead of being between 58 and 59 (original list), the new index 9 is between 56 and 57.


#8

I think it is the first one :slight_smile:


#9

If you want to try it to see which works better, here is the python:

# Enable Python support and load DesignScript library
import clr

_list = IN[0]
elems = [x for _,x in sorted(zip(IN[2],IN[1]))]
keep = IN[3]

if keep:
	indices = [v+i for i,v in enumerate(sorted(IN[2]))]
else:
	indices = sorted(IN[2])

for ind, elem in zip(indices, elems):
	_list.insert(ind, elem)

OUT = _list

I have only done a little bit of testing but it should be able to handle lists where the indices are not sorted as long as the items and indices are lined up.

The boolean True means that it will keep the original list index, meaning that the insert is based on the items, not the index. False is the opposite.

Edit: I was wrong about the last edit :joy:


#10

@Mark.Ackerley @kennyb6
The Python script does in the end exactly what I need. Cause somehow this RIE node does not insert anything on Index 0?


#11

It does for me…

image

If you look inside the node you’ll see similar Python to Kenny’s but with a bit of error catching.

It’s worth considering if you would rather manage Python code yourself, or put it in a custom node (you can update the node once and it updates in all your graphs) or if you’d rather use a package that someone else will update for you.

Cheers,

Mark


#12

But for me it does something strange?


Here you can see the outcome :slight_smile:

Thank you both!


#13

The RIE does the other version of what mine does. I don’t have the package installed so I can’t say what exactly it does but it basically works like the other scenario I mentioned.

That said, @Mark.Ackerley is right that the Python is more difficult to maintain and will not carry over into other graphs like a package would. I also did not put in any error catching for if the two lists are of different sizes or other problems.


#14

Sure, the RIE node is replacing one index with another.

As I now understand your need is to add a new item at the index?


#15

FYI, here’s the RIE code, thanks to @andre.abotnes!

OUT = []
add = 0

if isinstance(IN[1], list):
    ItemList = IN[1]
else:
    ItemList = [IN[1]]

if isinstance(IN[2], list):
    IndexList = IN[2]
else:
    IndexList = [IN[2]]

if len(IN[0]) > 0:

	if len(ItemList) == len(IndexList):
		for _ in range(0,len(IN[0])+len(IndexList)):
			if _ in IndexList:
				OUT.append(ItemList[IndexList.index(_)])
			else:
				OUT.append(IN[0][add])
				add += 1
	else:
		OUT = "Item list and Index list must be of equal length"
else:
	NewItemList = [x for _,x in sorted(zip(IndexList,ItemList))]
	for _ in NewItemList:
		OUT.append(_)

#16

NewItemList = [x for _,x in sorted(zip(IndexList,ItemList))]

I think we both copied from the same stackoverflow question. But his has error testing which will be much more useful than mine.


#17

I dont understand anything about python. So in the end which one is the best for me to use? :sweat_smile:


#18

If you don’t understand Python, you are probably better off using the one @Mark.Ackerley suggested. If the index is not in the right place, then you just need to add to the index list.


#19

And then learn a bit of Python :smiley: Just enough to understand what Kenny is doing…!


#20

I think we both copied from the same stackoverflow question. But his has error testing which will be much more useful than mine.

This might be the case @kennyb6 :joy:

Perhaps I should update the RIE package to have both methods too.