Considering
a = [None,1,2,3,None,4,None,None]
I would like to
a = [None,1,2,3,3,4,4,4]
Currently, I rudely forced him:
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 would like to get
a = [1,1,2,3,3,4,4,4]
running this right to the left. I am currently
def replaceNoneWithRightmost(val): return replaceNoneWithLeftmost(val[::-1])[::-1]
I'm not fussy about the place or creating a new list, but now it smells to me. I donβt see a way to save the temporary βlastβ value and use map / lambda, and nothing else comes to mind.
source share