Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat elements in list n times?

How do I repeat each element of a list n times and form a new list? For example:

x=[1,2,3,4]
n=3

Looking for:

[1,1,1,2,2,2,3,3,3,4,4,4]
like image 990
Alec Avatar asked Nov 25 '25 05:11

Alec


1 Answers

An inner argument to repeat is what I was looking for:

repeat([1, 2, 3, 4], inner = 3)
like image 75
Alec Avatar answered Nov 28 '25 00:11

Alec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!