Is there an easier way to achieve the below?
lst = []
repetitions = 3
for elem in range(3):
lst += [elem] * repetitions
this turns [0,1,2] into [0,0,0,1,1,1,2,2,2]
You could use a list comprehension with two loops:
>>> [elem for elem in range(3) for _ in range(repetitions)]
[0, 0, 0, 1, 1, 1, 2, 2, 2]
Using list comprehension:
print([item for item in lst for i in range(3)])
Using numpy.repeat:
lst = [0,1,2]
print(list(np.repeat(lst,3)))
OUTPUT:
[0, 0, 0, 1, 1, 1, 2, 2, 2]
[0, 0, 0, 1, 1, 1, 2, 2, 2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With