Proving the 'pull out property of conditional expectation

By definition of conditional expectation (discrete case)

$$ E[Y g(X)| X = x] = \dfrac{\sum_z z P(Y g(X) = z, X = x)}{P(X=x)} $$ where the sum is over all possible values of $Y g(X)$, and we're assuming $P(X=x) \ne 0$. Since we are requiring $X = x$, only the possible values when $X = x$ need to be considered, and these are $y g(x)$ where $y$ is a possible value of $Y$. If $g(x)$ happens to be $0$, then $z = 0$ is the only possible value and the sum is $0$. So in this case $E[Y g(X)|X=x] = 0 = g(x) E[Y|X=x]$. Otherwise, $P(Y g(X) = y g(x), X = x) = P(Y = y, X=x)$, so

$$ \eqalign{\dfrac{\sum_z z P(Y g(X) = z, X = x)}{P(X=x)} &= \dfrac{\sum_y y g(x) P(Y = y, X = x)}{P(X=x)}\cr &= g(x) \dfrac{\sum_y y P(Y = y, X = x)}{P(X=x)} \cr &= g(x) E(Y|X=x)}$$

Actually, there is a bit of a caveat: we need to assume $E(Y|X=x)$ exists, otherwise the right side $g(x) E[Y|X=x]$ is undefined. Of course if $g(x) \ne 0$ the left side would also be undefined, but in the case $g(X) = 0$ (where we don't want to assign a value to $0 \times undefined$) the left side would be $0$.