{"record":{"id":"100a67ab558ecf9c","repo":"TheAlgorithms/Python","slug":"list-index-out-of-range","errorCode":null,"errorMessage":"list index out of range","messagePattern":"list index out of range","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"data_structures/arrays/sparse_table.py","lineNumber":81,"sourceCode":"    >>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3]), 0, 4)\n    0\n    >>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3]), 4, 6)\n    3\n    >>> query(build_sparse_table([3, 1, 9]), 2, 2)\n    9\n    >>> query(build_sparse_table([3, 1, 9]), 0, 1)\n    1\n    >>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3]), 0, 11)\n    Traceback (most recent call last):\n    ...\n    IndexError: list index out of range\n    >>> query(build_sparse_table([]), 0, 0)\n    Traceback (most recent call last):\n    ...\n    ValueError: empty number list not allowed\n    \"\"\"\n    if left_bound < 0 or right_bound >= len(sparse_table[0]):\n        raise IndexError(\"list index out of range\")\n\n    # highest subset length of power of 2 that is within range [left_bound, right_bound]\n    j = int(log2(right_bound - left_bound + 1))\n\n    # minimum of 2 overlapping smaller subsets:\n    # [left_bound, left_bound + 2 ** j - 1] and [right_bound - 2 ** j + 1, right_bound]\n    return min(sparse_table[j][right_bound - (1 << j) + 1], sparse_table[j][left_bound])\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n    print(f\"{query(build_sparse_table([3, 1, 9]), 2, 2) = }\")\n","sourceCodeStart":63,"sourceCodeEnd":96,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/arrays/sparse_table.py#L63-L96","documentation":"Raised by query() in data_structures/arrays/sparse_table.py when left_bound < 0 or right_bound >= len(sparse_table[0]) — i.e. the query range falls outside the array the table was built from. It raises IndexError with the message 'list index out of range', deliberately mimicking a native list indexing failure. Bounds are inclusive on both ends.","triggerScenarios":"Calling query(build_sparse_table([8,1,0,3,4,9,3]), 0, 11) where the table has 7 elements (valid range 0..6), or any negative left_bound.","commonSituations":"Inclusive-vs-exclusive bound confusion (passing right_bound == n), ranges from user input or coordinate math not clamped to the array, or reusing a table built for one array with bounds computed against another.","solutions":["Clamp/validate bounds against the source array length: 0 <= left <= right < len(arr).","Remember both bounds are inclusive — the last valid right_bound is len(arr) - 1.","Build the table and compute query bounds from the same array variable to avoid size drift."],"exampleFix":"# before\nquery(table, 0, 11)  # table built from 7 elements\n\n# after\nquery(table, 0, 6)  # inclusive bounds within 0..6","handlingStrategy":"validation","validationCode":"n = len(values)  # the array the table was built from\nif not 0 <= left <= right < n:\n    raise ValueError(f'bounds outside 0..{n-1}')\nresult = query(table, left, right)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Both bounds are inclusive","Compute bounds against the same array the table was built from","Clamp user-supplied ranges before querying"],"tags":["value-validation","sparse-table","rmq","index","off-by-one"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}