Passing a list of indices instead of single index to Dataset returns error

I tried to pass a list of indices to the dataset however the __getitem__ function in my custom dataset The list of indices is from BatchSampler. But I’m getting a Type Error list indices must be integers or slices, not list when running the code.
How do I solve this issue?

class MyData(Dataset):
    def __init__(self, data, transform = None): = data
        self.transform = transform

    def __getitem__(self, index):

        img, label =[index]  #<------ The error occurs here

        if self.transform is not None:
            img = self.transform(img)
        return img, label

    def __len__(self):
        return len(

training_set = MyData(trainset,transform = train_aug)
sampler =,

trainloader = DataLoader(

Instead of trainloader = DataLoader(training_set, sampler=sampler), try trainloader = DataLoader(training_set, batch_sampler=sampler).

If i change it to sampler, I’m requesting index instead of a batch. or I’m misunderstanding things here?

batch_sampler yields a list of lists where each list represents batch indices and sampler yields an index of next item (these items are then combined to form a batch according to the batch_size).