The mean and standard deviation are themselves pulled by outliers. The modified z-score (Iglewicz & Hoaglin) uses the median and the median absolute deviation:
Implement modified_z(x) and robust_outliers(x, threshold=3.5) (flags threshold). If MAD is 0, every point that differs from the median gets and points equal to it get 0.