How can I increase the size of, and pad, a python list?
list, python
Solution
>>> x = [1,2,3,4]
>>> [n for n in x for _ in range(4)]
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
`itertools.repeat` is indeed semantically cleaner, thanks, Steven:
from itertools import repeat
[repeated for value in x for repeated in repeat(value, 4)]
Problem
Say I have this list: ``` [1,2,3,4] ``` and I want: ``` [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4] ``` What is the best way of doing this? My current method is to create a new list: ``` x = [1,2,3,4] y = [[n]*4 for n in x] ``` This gives: ``` [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]] ``` Which seems close but no cigar... Can anyone help?