Solution (source code)

= Solution

A <partition of a sample space> is a finite or countable family of pairwise disjoint <events> $B_i$ whose union is the whole <sample space>. For an integrable <random variable>, the partition form of the <law of total expectation> is
$$
\boxed{\mathbb E[X]=\sum_{i:\mathbb P(B_i)>0}\mathbb P(B_i)\mathbb E[X\mid B_i].}
$$
Zero-probability cells contribute zero and are omitted because elementary event-conditioning on them is undefined. The formula also holds with extended nonnegative values when $X\ge0$. Integrability makes the signed series legitimate in the finite-expectation case.