# Understanding a Substep of the Proof for the Law of Total Variance

In the proof for the Law of Total Variance, the following lemma seems to be appealed to (when going from the 2nd to the 3rd step of the proof):

$$E[E[Y^2 \mid X]] = E[\text{Var}[Y \mid X] + [E[Y \mid X]]^2]$$

Where does this come from and/or what justifies it?

$$E[Y^2\mid X]=\operatorname{Var}[Y\mid X]+E[Y\mid X]^2\;,$$
$$E[Y^2]=\operatorname{Var}[Y]+E[Y]^2\;.$$
Discrete version: $$\text{Var}[Y|X]=\sum_{y}(y-E[Y|X])^2p_{Y|X}(y|x)\\=\sum_yy^2p_{Y|X}(y|x)-2E[Y|X]\sum_y yp_{Y|X}(y|x)+E[Y|X]^2\sum_{y}p_{Y|X}(y|x)\\ =E[Y^2|X]-2E[Y|X]^2+E[Y|X]^2=E[Y^2|X]-E[Y|X]^2\\ \therefore E[Y^2|X]=\text{Var}[Y|X]+E[Y|X]^2$$