Insert Items using Multiple Indices is not behaving as expected

Hi everyone,

I was doing a simple operation. That is, I have a list of numbers, and I want to insert an empty value/null at certain indices. however, I get a weird result as shown in the screenshot.

Also, you’ll see that I did it in a weird way that I consider not smart and not fully parametric, but at least it gets the job done and The resulting list is exactly what I want. However, in my original case, since I want to insert an item at a large number of indices, the process is not going to be efficient.

My question is, why is the Insert Items not behaving as I expect?, and is there a way to do what I expect smarter than I did?

Thank you guys for your help.

Insert Items using Multiple Indices.gh (9.4 KB)

Each time you insert a item the indicies increase by one.

First insert index+0
Second insert index+1
Third insert index +2
etc.

So your index 3+0,5+1,8+2 etc.

You could sum each item number with his index number but are you sure you want a increasing list or just a replacement of items in a list.

Actually, I noticed this pattern.

Yes, no problem with the list being increased.

I’m looking for a formula or an expression that could adjust an input of a list of indices, so the output would be the adjusted/right indices and that would help to get what I want (the expected result as shown in the screenshot). Is that possible?

As i said just use the list index and use math + to add each index to each item index that you want.

Or use replace item…in this case the list length would not increase.

I can show you in 10h when i am back at my computer.

actually really not what I was also expecting: it works with subtraction, probably it’s too early in the morning for me to clearly understand why

Insert Items using Multiple Indices_Re.gh (14.0 KB)

2 Likes

I think you r looking for replace item instead of insert item

1 Like

Thanks, Rajeev,

Actually, I want the result list to be increased, no problem with that. I want to match it with another list, so eventually, the list I want to adjust will have the same amount of items as the other list. That’s because I’m trying to export them to Excel, and you know, all lists should have the same number of items \ same length.

So, inserting Empty values where I want, like indices (3,8,5 and there are a lot more than these) will get the job done properly.

1 Like

matching the data with the other list as you mentioned

1 Like

You got it Inno, that’s it!.

Here’s the proof of my actual scenario using your subtraction-based approach. I have a list of generated indices. These indices will be inserted into a specific list as “-”, that means they will increase the number of items in order to reach a total of 122 items. As you can see in the screenshot, the resulted list is exactly 122 and the indices were exactly in place.

I made a closer look to the end of the list, but the rest of the indices are all good. Thanks again.

ok, I thought about that for 10 minutes with a good drink :slight_smile:

the issue stems from the thing that with List Items you specify the insertion index, not the final index of the inserted items

so -of course- if you want to end.up with something at indexes 3, 5, 8 you have to account for the other items in the list to slide one position at each insertion

for instance, base list 0 to 9

if you say “I want to add items at 3, 5, 8” then Insert Items works exactly as expected:

if was enlighting to look at that with this intermediate step (which is pure text edited by hand) where the nulls are actually placed into the list at the right location, but of course two items can’t have same index so each of them slide one position:

but again, Insert Item using Multiple Indices just does behave as expected :sunglasses: you just need to input the insertion index, not the final index of the inserted items :+1:

1 Like