Given
a = [None,1,2,3,None,4,None,None]
I'd like
a = [None,1,2,3,3,4,4,4]
Currently I have brute forced it with:
def replaceNoneWithLeftmost(val):
last = None
ret = []
for x in val:
if x is not None:
ret.append(x)
last = x
else:
ret.append(last)
return ret
Finally, I'd like to get to
a = [1,1,2,3,3,4,4,4]
by running this right to left. Currently I have
def replaceNoneWithRightmost(val):
return replaceNoneWithLeftmost(val[::-1])[::-1]
I'm not fussy about inplace or create a new list, but right now this smells to me. I can't see a way to store a temporary 'last' value and use map/lambda, and nothing else is coming to mind.