# Custom Dataset \_\_getitem\_\_ is receiving a list from DataLoader

**URL:** <https://discuss.pytorch.org/t/custom-dataset-getitem-is-receiving-a-list-from-dataloader/217592>\
**Category:** vision\
**Created:** [March 8, 2025, 12:27pm UTC](https://discuss.pytorch.org/t/custom-dataset-getitem-is-receiving-a-list-from-dataloader/217592 "2025-03-08T12:27:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gabrichilleron](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gabrichilleron/32/76040_2.png) [@Gabrichilleron](https://discuss.pytorch.org/u/Gabrichilleron)\
**Post date:** [March 8, 2025, 12:27pm UTC](https://discuss.pytorch.org/t/custom-dataset-getitem-is-receiving-a-list-from-dataloader/217592/1 "2025-03-08T12:27:38Z")

</div>

Im new using pytorch. I have a custom dataset which is reading images from disk, it looks like this:

```auto
class SaffronDataset(Dataset):
    def __init__ (self, base_dir, transform=None, target_transform=None):
        self.images_dir = os.path.join(base_dir, 'raw')
        self.labels_dir = os.path.join(base_dir, 'segmentation')
        self.transform = transform
        self.target_transform = target_transform
        self.images = sorted([os.path.join(self.images_dir,x) for x in os.listdir(self.images_dir)])
        self.labels = sorted([os.path.join(self.labels_dir,x) for x in os.listdir(self.labels_dir)])
        

    def __len__ (self):
        return len(self.images)

    def __getitem__ (self, idx):
        images = Image.open(self.images[idx])
        labels = Image.open(self.labels[idx])
        if self.transform:
            images = self.transform(images)
        if self.target_transform:
            labels = self.transform(labels)
        return images, labels

```

I have a basic DataLoader too:

```auto
data_loader = DataLoader(data, batch_size=2, shuffle=True)

```

When i try to iterate the dataloader, im getting the error **TypeError: list indices must be integers or slices, not list**. The problem is that **getitem()** function is receiving a list instead of a single index and i dont understand why, anyone can help me with that?

---

<div class="post-metadata">

**Author:** ![lostalot](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/858c86/32.png) [@lostalot](https://discuss.pytorch.org/u/lostalot)\
**Post date:** [March 8, 2025, 3:30pm UTC](https://discuss.pytorch.org/t/custom-dataset-getitem-is-receiving-a-list-from-dataloader/217592/2 "2025-03-08T15:30:51Z")

</div>

Might you consider printing out idx so you can see what it contains?

---

<div class="post-metadata">

**Author:** ![Gabrichilleron](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gabrichilleron/32/76040_2.png) [@Gabrichilleron](https://discuss.pytorch.org/u/Gabrichilleron)\
**Post date:** [March 8, 2025, 3:40pm UTC](https://discuss.pytorch.org/t/custom-dataset-getitem-is-receiving-a-list-from-dataloader/217592/3 "2025-03-08T15:40:38Z")

</div>

Idx contains a list with a number of index equals to the bacth\_size. For example, in my case with batch\_size = 2, idx can take [2,3] value.  
The problem is that if I take it into account and deal with it in the **getitem** () function, returning a list of images. Then I get an error because a single tuple (image, label) is expected as output.

---

<div class="post-metadata">

**Author:** ![UMAR\_MASUD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/umar_masud/32/68863_2.png) [@UMAR\_MASUD](https://discuss.pytorch.org/u/UMAR_MASUD)\
**Post date:** [March 14, 2025, 5:32am UTC](https://discuss.pytorch.org/t/custom-dataset-getitem-is-receiving-a-list-from-dataloader/217592/4 "2025-03-14T05:32:05Z")

</div>

Hi, that’s an unusual case. The `idx` value in ` __getitem__ (self, idx)` function should be a single integer value in the range `[0,len(dataset)-1]` meant for a single sample of the dataset. Getting a list means something is overriding the default behaviour maybe a custom collate function. Do you mind sharing some more code for context and any errors, or print statements?
