I have discovered this very close to my question:
Adding two items at a time in a list comprehension
But what if I need to switch between single or double, like:
original = list(range(10))
required = [0,0,1,2,2,3,4,4,5,6,6,7,8,8,9]
attempt1 = sum([[x,x] if x%2 == 0 else [x] for x in original],[])
attempt2 = [i for x in original for i in ([x,x] if x%2 == 0 else [x])]
sum
seems slow, and list comprehension is hard to read. Neither of them makes me feel simple and good.
Is there a better way to do it? Or just abandon the one-line way? Or convince me if one of them is really good style.