I have a list of booleans
l = [False, False, False, True, False, False, False]
that I want to turn into
l_new = [False, False, False, True, True, True, False]
That means, whenever there is a True in my list I want to switch the two (for example) following values to true. My solution is
def lagged_effect(l, lag):
l_new = []
L_iter = iter(l)
for elem in L_iter:
if elem == True:
l_new.extend([True]*lag)
if lag == 1:
next(L_iter)
if lag == 2:
next(L_iter)
next(L_iter)
if lag == 3:
next(L_iter)
next(L_iter)
next(L_iter)
if lag == 4:
next(L_iter)
next(L_iter)
next(L_iter)
next(L_iter)
if lag > 4:
print("not defined")
if elem == False:
l_new.append(False)
return l_new
print(l_new)
lagged_effect(l, lag=2)
Since I want to implement this more often, I was wondering if there might be a more compact and efficient solution. Especially the next implementation annoys me.