replicating elements in list
list, python, replication
Solution
My 2 cents :
Another way to achieve this would also be to take advantage of the fact you can multiply lists in Python.
>>> l = [1, 2, 3]
>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
A simple call to the `sorted` function allows to sort it back :
>>> b = 3
>>> l = [1, 2]
>>> sorted(l * b)
[1, 1, 1, 2, 2, 2]
Problem
Say I have this ``` b = 3 l = [1, 2] ``` I want to modify l so that each element appears as many times as b. So that: ``` l = [1, 1, 1, 2, 2, 2] ``` I used this: ``` for x in l: for m in range(b): l.append(x) ``` But it resulted in an infinite loop. Any help would be appreciated. I would prefer you guys to give ideas rather than give me the code. Thanks.