# Insert Item to end of list at index

Hello all,

I am trying to insert items to the end of a list at an index. I cannot seam to get it to work though. Sorry if my poor explanation, I am having a hard time trying to explain it.

I have a list with sub list.
List of Index’s
and a list of items to be added. I want one item to be added to to the end of each sub list at of the corresponding index. Right now the whole list of items creates a new sub list.

Thank for the help,

Could you sketch what you would like your result to be?

Thanks for the quick reply I will add something tomorrow on my way out the door.

@Steven is this what you’re looking for?

2 Likes

I think he wants the second list inserted only at certain indexes (Insert node’s index). Don’t have python open right now and I know there is a better way but something like this would work assuming index is always in numerical order:

``````list1 = IN[0]
list2 = IN[1]
indexes = IN[2]

count = 0

for i in range(len(list1)):
if i == indexes[count]:
list1[i].append(list2[count])
count += 1
``````

or try this:

``````list1 = IN[0]
list2 = IN[1]
indexes = IN[2]

for i in range(len(indexes)):
list1[indexes[i]].append(list2[i])``````
3 Likes

@kennyb6 solution should work.

You could also try this:

``````list1 = IN[0]
list2 = IN[1]
indexes = IN[2]

for i,j in zip (list2,indexes):list1[j].append(i)

OUT=list1
``````

5 Likes

Can anybody make it work with nodes only? I can’t make it work with OOTB List.ReplaceItemAtIndex node.

1 Like

An OOTB solution …

5 Likes

this was a fun game doing it with OOTB nodes
Home.dyn (7.2 KB)

5 Likes

…and just for the game
Home2.dyn (4.0 KB)

3 Likes

Glad you were able to have fun with this one. I am not sure what to mark as a solution since you all have given really good ones.

Thanks every one this is very helpful and a great learning experience.

Steven

2 Likes

mark the first that solved your issue, i think all others was just having fun

1 Like

I did Vikram’s OOTB solution since it is the most straight forward and done without code. I really like the coded work flows but for others looking for the solution I figured it best.

1 Like

ok.This is weird!..I just changed the data and it does’t work?

@salvatoredragotta Works with alterations.

1 Like

If the builtin Reorder node worked and if your data does not have any null values, it should be as simple as:

2 Likes

Just for the record, my solution worked with null values using only OOTB nodes

what about cases with consecutive indices?

Let’s say we’re interested in extending only the sublists at {1, 2, 4} ?

Null values are very troublesome, and should not be used under any circumstances. Using a custom node was a kind of cheating since a custom node essentially is the same as using coding which is so much easier. I would never have done this in OOTB nodes, and there is many good examples how much easier it is to use few lines of code to do it.

However, If we had a looping OOTB node in dynamo there was user-friendly/useful then I would have used that, but as you have written in a post some years ago, it is mysterious. I tried using the LoopWhile but had to give up as I do every time. Python looping is user-friendly/useful.

So only in this case where I had to combine a value and a null value was it easy, and that was the task

1 Like

Another (less convoluted) OOTB approach…
``````a = List.GetItemAtIndex(lst,idx);