Repeat item with Python

Hello I would like to achieve this node of dynamo in python

image

I got a flatten list with another list with number of times to repeat each index of the flatten list, so the result is sublists with repeated items, for example:
image

thanks

1 Like
x = IN[0]

n = 3

OUT = [item for item in x for i in range(n)]

that i tried from “stackflow” page

it does not work because asking a number in the range and I put a list of numbers
image

@ruben.romero can you show your list structure?

image

Like this?

OUT = [[itm]*n for itm,n in zip(IN[0],IN[1])]

1 Like

A slight alteration to @EdsonMatt appraoch:

import sys

i = "item"
a = [1,5,1,4]

OUT = [[itm]*n for itm,n in zip([i] * len(a),a)]

image

2 Likes

Generally you’ll need to iterate (for x in listobject) if you wan’t to run any example people show you on Dynamo forums across a list object. If you’re working across lists in parallel, the zip() option is handy.

Some fairly well comprehended versions above, but this is the expanded version. I generally find these easier to read versus comprehended (essentially implied) statements ([x for x in thing]).

rptList = IN[0]
numList = IN[1]

outList = []

for rpt,num in zip(rptList,numList):
	out = []
	for n in range(num):
		out.append(rpt)
	outList.append(out)
	
OUT = outList

4 Likes

Man thanks @GavinCrump, tested and working well. Athough finally I used the @EdsonMatt single line code.

1 Like