You need if and else inside the list comprehension. It goes like this [i if ... else ... for i in lst ].