I have the following piece of code, with the goal of repeating every element of a 2D list a certain number of times:
empty =[]
l = [[1,2,3], [4,5,6]]
n = [2,3]
for i, _as in enumerate(l):
for _a in _as:
for _n in range(n[i]):
empty.append(_a)
empty
> [1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
I would like to try and write this in a single line list comprehension format.
I have made an attempt using using:
empty = [
[
[_a]*n[i] for _a in _as
]
for i, _as in enumerate(l)
]
empty
> [[[1, 1], [2, 2], [3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]]]
How may I correct the above code to provide the desired result?