{"record":{"id":"c7842191ffdbba0c","repo":"krahets/hello-algo","slug":"index-out-of-bounds-c78421","errorCode":null,"errorMessage":"Index out of bounds","messagePattern":"Index out of bounds","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"en/codes/python/chapter_array_and_linkedlist/my_list.py","lineNumber":30,"sourceCode":"        \"\"\"Constructor\"\"\"\n        self._capacity: int = 10  # List capacity\n        self._arr: list[int] = [0] * self._capacity  # Array (stores list elements)\n        self._size: int = 0  # List length (current number of elements)\n        self._extend_ratio: int = 2  # Multiple by which the list capacity is extended each time\n\n    def size(self) -> int:\n        \"\"\"Get list length (current number of elements)\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"Get list capacity\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"Access element\"\"\"\n        # If the index is out of bounds, throw an exception, as below\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"Update element\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"Index out of bounds\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"Add element at the end\"\"\"\n        # When the number of elements exceeds capacity, trigger the extension mechanism\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"Insert element in the middle\"\"\"","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/python/chapter_array_and_linkedlist/my_list.py#L12-L48","documentation":"IndexError 'Index out of bounds' raised by MyList.get. The list only exposes indices [0, _size); anything below 0 or >= _size is rejected even if the backing array has spare capacity up to _capacity.","triggerScenarios":"Calling lst.get(index) with index < 0 or index >= lst.size().","commonSituations":"Using the backing capacity instead of size as the upper bound; off-by-one in a for-range; reading an index after a remove shifted the elements.","solutions":["Validate 0 <= index < lst.size() before get.","Use lst.size() (not capacity()) as the loop bound.","After remove/insert, recompute indices rather than reusing them."],"exampleFix":"// before\nval = lst.get(i)  # i may equal size\n// after\nif 0 <= i < lst.size():\n    val = lst.get(i)","handlingStrategy":"validation","validationCode":"def safe_get(lst, index):\n    if 0 <= index < lst.size():\n        return lst.get(index)\n    raise IndexError(f\"index {index} out of [0, {lst.size()})\")","typeGuard":"def valid_list_index(lst, index) -> bool:\n    return isinstance(index, int) and 0 <= index < lst.size()","tryCatchPattern":"try:\n    val = lst.get(i)\nexcept IndexError:\n    val = None","preventionTips":["Use size() (not capacity()) as the upper bound.","Recompute indices after insert/remove.","Wrap reads with a bounds-checking helper."],"tags":["dynamic-array","index-error","validation","get"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}