The “clean” solution would be to iterate the dataset once before the training, store all invalid indices in a list or any other container and remove these invalid samples afterwards.
During training you could sample another index randomly inside __getitem__, but this would yield these randomly samples multiple times during training, which might not be desirable.