I have a nested list that looks like this:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
I want to duplicate each element in each inner list by 3 and place them side to side as such:
[[1, 1, 1, 2, 2, 2, 3, 3, 3], [4, 4, 4, 5, 5, 5, 6, 6, 6], [7, 7, 7, 8, 8, 8, 9, 9, 9]]
This is my current solution:
from itertools import repeat
x2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
ls = []
for i in x2:
ls.append([x for item in i for x in repeat(item, 3)])
ls
>>> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[5, 5, 5, 6, 6, 6, 7, 7, 7],
[9, 9, 9, 10, 10, 10, 11, 11, 11],
[4, 4, 4, 8, 8, 8, 3, 3, 3]]
Is there any way to make it faster?