Object is enumerable but not indexable?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Being enumerable and being indexable are different capabilities, even though they are often confused in everyday coding. Enumerable data can be iterated in sequence, while indexable data supports random access by position. Knowing this distinction helps you avoid conversion overhead, one-shot iterator bugs, and incorrect API contracts.
Core Difference in Access Semantics
Enumerable means values can be consumed one after another. Indexable means you can request a specific position directly, such as the fifth element.
Common enumerable but not indexable sources:
- JavaScript generators
- JavaScript sets
- stream readers
- Python iterators and generators
These sources are excellent for streaming pipelines but cannot guarantee direct position access.
JavaScript Generator Example
The generator is iterable, but not array-like.
Set Example and Controlled Conversion
A Set is iterable and preserves insertion order, yet bracket indexing still does not apply.
If you truly need positional access, convert intentionally:
Do this only when index operations justify the memory cost.
Streaming-Friendly Access Without Full Materialization
Sometimes you only need one element from an iterable. Full conversion to array may be wasteful.
This pattern keeps memory bounded and supports large or infinite iterables.
Python Parallel for the Same Concept
Need a specific position without full list conversion:
This preserves streaming behavior and avoids unnecessary memory growth.
API Design Guidance
When designing function signatures, be explicit:
- accept iterables when sequential scanning is enough
- require arrays or lists only when random access is required
JavaScript example:
These two functions have different requirements and should not be treated as interchangeable.
One-Shot Iterable Hazards
Some iterables are consumable only once. If a helper function iterates through the entire source for logging or metrics, the next consumer may receive nothing.
Use clear ownership rules:
- either consume once and document it
- or cache materialized results when multiple passes are required
Unexpected exhaustion is a common production bug in streaming pipelines. When performance tuning, profile allocation and iteration counts before converting iterable sources to arrays, because silent materialization can dominate both memory and latency. A small utility that records whether inputs are arrays, sets, or generators can prevent accidental indexing assumptions from spreading through shared helper libraries.
Common Pitfalls
- Assuming every iterable supports bracket indexing.
- Converting large streams to arrays for trivial access patterns.
- Forgetting that some iterables are one-shot and cannot be reused.
- Designing APIs with unclear expectations about access semantics.
- Mixing random-access and streaming assumptions in the same algorithm.
Summary
- Enumerable and indexable represent different data access capabilities.
- Generators and sets are iterable but not directly indexable.
- Convert to indexed collections only when random access is necessary.
- Prefer bounded streaming helpers for large or infinite inputs.
- Make API requirements explicit to prevent hidden performance and correctness issues.
Related reading
- Object of custom type as dictionary key
- ObservableCollection vs. List
- Obtain forest out of tree with even number of nodes
- Obtain importance of individual trees in a RandomForest
- offsetting an html anchor to adjust for fixed header
- OpenAI GPT-2 model use with TensorFlow JS
- Obtain input_array and output_array items to convert model to tflite format
- Obtaining a powerset of a set in Java

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.