Problem with Dataloader object not subscriptable
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
TypeError: 'DataLoader' object is not subscriptable means your code is treating a PyTorch DataLoader like a list, for example with loader[0]. That fails because DataLoader is designed to be an iterable over batches, not a random-access container.
The fix depends on what you actually wanted. If you wanted one batch, use iter and next. If you wanted one sample, index the underlying dataset instead.
Why DataLoader Does Not Support Indexing
A Dataset and a DataLoader have different jobs:
- '
Datasetknows how to return one sample' - '
DataLoaderknows how to batch, shuffle, and iterate over samples'
That distinction is why this works:
but this does not:
The loader may be shuffling data, using multiple workers, pinning memory, or reading from an IterableDataset, so random-access subscription is not part of its contract.
The Normal Way: Iterate Over the Loader
For training and evaluation, use a loop:
That is the intended usage. Each iteration yields one batch.
If You Want Only One Batch
Sometimes you are just inspecting data in a notebook and want the first batch. In that case:
This is the right mental model:
- '
iter(loader)creates a batch iterator' - '
next(...)pulls one batch from that iterator'
It is the closest equivalent to loader[0], but it respects the iterable design.
If You Want One Sample
If the code really wants a single sample rather than a batch, index the dataset:
That keeps responsibilities clean. The dataset answers sample-level access; the loader answers batch-level iteration.
This is especially important if you later change batch size or turn on shuffling. A dataset sample and a loader batch are different concepts.
A Common Helper-Function Bug
This error often appears because a helper function expects a dataset but receives a loader by mistake:
A safer version makes the expectation explicit:
Small naming differences like dataset versus loader prevent a lot of confusion in larger training codebases.
IterableDataset Makes Indexing Even Less Appropriate
Some datasets are not indexable at all. IterableDataset is meant for streaming data where samples are produced sequentially:
In this case, random access makes no sense, which is another reason PyTorch keeps DataLoader focused on iteration rather than subscription.
Avoid list(loader) for Inspection
A common workaround is:
That works for tiny datasets, but it materializes every batch in memory. For real training jobs, that is wasteful and can be very slow. Prefer next(iter(loader)) when you only need one batch preview.
Common Pitfalls
- Writing
loader[0]because the code mentally treats a loader like a list. - Forgetting whether the current variable is a dataset or a loader.
- Converting the whole loader to a list just to inspect one batch.
- Expecting a loader to provide stable batch order when
shuffle=True. - Ignoring the difference between sample-level access and batch-level iteration.
Summary
- '
DataLoaderis iterable, not subscriptable.' - Use
for ... in loaderfor normal batch processing. - Use
next(iter(loader))when you need one batch quickly. - Use
dataset[index]when you need one specific sample. - Keep dataset and loader responsibilities separate to avoid this error entirely.
Related reading
- Problem with missing and unexpected keys while loading my model in Pytorch
- Pytorch - Concatenating Datasets before using Dataloader
- Pytorch - Concatenating Datasets before using Dataloader
- pytorch - connection between loss.backward and optimizer.step
- Problem with Dropout version Google Colab
- Problem with escaping password with special characters in Kubernetes cloudsql
- PyTorch - How to get learning rate during training?
- Pytorch - Using more GPUs and increasing batch size makes training slower in DistributedDataParallel
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.