I'm struggling with list comprehensions
. I need to write a function that will add 0 for every N numbers.
The function looks like this:
def makeSpace(list, space):
list = [list.insert(i, 0) for i in list if ....]
return list
Example, Im passing makeSpace(list,2)
and if my list looked like: 1,2,3,4,5,6
then after the function should return 1,2,0,3,4,0,5,6,0
Can someone tell me how to make it?
PS: If for loop is better for it, it can be traditional for loop
I found in a duplicate
question something and change in on my own:
def fun(lst, space):
i = space
while i < len(lst):
lst.insert(i, 0)
i += space+ 1
return lst
But it does not add number at the end, for example for every 3 space:
[1, 2, 3, 0, 4, 5, 6]
it should add also after 6
What should I change?