3

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]
Alec
  • 3,509
  • 1
  • 30
  • 42

2 Answers2

3

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

repeat([1, 2, 3, 4], inner = 3)
Alec
  • 3,509
  • 1
  • 30
  • 42
1

Also list comprehension:

x = [1,2,3,4]
n = 3
result = [i for i in x for j in 1:n]
张实唯
  • 2,776
  • 12
  • 25