Insert string after after every item in list (improve code)

Hi All,

I’m hoping I can get some tips to make my code better. I think the picture shows what I tried to achieve.

Thanks!

document = IN[0]
element = IN[1]

count = len(element[0])
e_index0 = element[0]
repeat0 = [document[0]] * count
lst = [e_index0,repeat0]
zip0 = zip(*lst)
OUT0 = [item for sublist in zip0 for item in sublist]

count1 = len(element[1])
e_index1 = element[1]
repeat1 = [document[1]] * count1
lst1 = [e_index1,repeat1]
zip1 = zip(*lst1)
OUT1 = [item for sublist in zip1 for item in sublist]

count2 = len(element[2])
e_index3 = element[2]
repeat2 = [document[2]] * count2
lst2 = [e_index3,repeat2]
zip2 = zip(*lst2)
OUT2 = [item for sublist in zip2 for item in sublist]

OUT = (OUT0, OUT1, OUT2)

Using loops is a good way to do this.
How about this:

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.
numbers = IN[0]
strings = IN[1]
listout = []

for n,s in zip(numbers,strings):
	sublist = []
	for x in n:
		sublist.append(x)
		sublist.append(s)
	listout.append(sublist)

#Assign your output to the OUT variable.
OUT = listout
3 Likes

Thank this is really helpful.