Lacing on replacing item on index

#1

hello, I am trying to replace a list of items at a list of indices into a list of elements. I am sure I am having problems with lacing, but i cant figure out what it is. Can anyone help me please? I originally had a list of 49 elements. I removed the 12 elements that I want to replace and then am trying to insert the new elements into those same places. Does that make sense? I tried the replace items at index but had the same lacing issues. Please advise.

How to reduce a list by using Remove Item at Index
#2

Is there a reason you are not using the List.ReplaceItemAtIndex node, rather than removing the 12 items that you wanted to replace first?

Also, I don’t think this is a lacing issue, since your list is 37 items and 12 is less than 37.

What are you feeding into the “index” input on the Insert node?

1 Like
#3

It’s not a lacing issue. It’s an index issue. You have 37 items but you have indices outside of that range.
What you really want is to replace the item at the specified index, but I’m guessing you ran into issues with that so tried this instead. I would suggest using Python if you’re trying to replace multiple items at multiple indices in the same list.

1 Like
#4

I tried the replace items at index nodes already. Same issues. I am feeding the list of indicies into the index node. (you can see them in the “Flatten” node connected to the “Index” import above. :confused

#5

#6

Nick,

Yes, that is exactly what I am trying to do! unfortunately that I dont have any python skills. Are there any packages out there that can do this? it seems like it would be a fairly common issue. no? Thanks

@erfajo
Thanks, but what I am really trying to do is:
image

#7

Clockwork has ReplaceItemAtIndex+ that does just this. Or you can look at this Python code that does the same thing.

dataEnteringNode = IN
list = IN[0]
elements = IN[1]
indices = IN[2]
count = 0
i = 0
out = []
for item in list:
	try:
		if count == indices[i]:
			out.append(elements[i])
			i = i+1
			count = count+1
		else:
			out.append(item)
			count = count+1		
	except:
		out.append(item)
		count = count+1

OUT = out
3 Likes
#8

@Nick_Boyts
Jackpot! Thanks Nick!
image

#9

Andreas actually uses a much nicer method for replacing items.

dataEnteringNode = IN
list = IN[0]
elements = IN[1]
indices = IN[2]

for e,i in zip(elements,indices):
	list[i]=e

OUT = list

The simplicity of Python still amazes me at times.

1 Like
#10

Its not working for me. Do I have to flatten all the lists going into these python scripts?

#11

@Nick_Boyts your last one will go out of index… you need to pad

list1 = IN[0]
list2 = IN[1]
index = IN[2]

def pad(var, length):
    return var[:length] + [None]*(length - len(var))

pad(list1, len(list1) + len(list2) )
for idx, item in zip(index,list2):
    list1.insert(idx, item)

OUT = list1
1 Like
Python Nodes Basics
#12

Those are both setup for one list of elements with one one list of replacements. You’d have to modify the code a bit to take multiple lists.

#13

#14

Seems to work for me with a simple test case.
image

#15

yeah it did for me too. I just think its a flattening or a lacing issue

#16

that’s because you have the length defined before you start changing items… if you have a list of length 6 and want to add 4 items at specific index… you need to pad the list to size 10

I read the problem as… a list of 37 items that need 12 items to be added (at specific index), so the list end at 59 elements

#17

It’s hard to tell what the issue is when we can’t see any of your inputs. You might have more control using the node from Clockwork.

#18

Gotcha.
But for replacing items you should be fine as long as your indices are within range.

#19

Sorry,
Here you go its really hard with such a small screen:

#20

I am sure that will go wrong…
the list is of 37 elements and 12 to be added at specific index, not to be replaced… but i might read the question wrongly