blade:

Is there a way to use dataloader to handle this?


Yes, you could create a custom sampler, draw the indices using your custom logic, and let the DataLoader pass these indices to the Dataset.__getitem__. You might also want to check the BatchSampler as it can pass a batch of indices to __getitem__.