{"record":{"id":"a122fcd36ecc5237","repo":"TheAlgorithms/Python","slug":"binary-search-insert-only-accepts-either-a-list","errorCode":null,"errorMessage":"binary_search_insert() only accepts either a list, range or str","messagePattern":"binary_search_insert\\(\\) only accepts either a list, range or str","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"other/number_container_system.py","lineNumber":102,"sourceCode":"        >>> NumberContainer().binary_search_insert([0,1,3], 2)\n        [0, 1, 2, 3]\n        >>> NumberContainer().binary_search_insert([-5, -3, 0, 0, 11, 103], 51)\n        [-5, -3, 0, 0, 11, 51, 103]\n        >>> NumberContainer().binary_search_insert([-5, -3, 0, 0, 11, 100, 103], 101)\n        [-5, -3, 0, 0, 11, 100, 101, 103]\n        >>> NumberContainer().binary_search_insert(range(10), 4)\n        [0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9]\n        >>> NumberContainer().binary_search_insert(\"abd\", \"c\")\n        ['a', 'b', 'c', 'd']\n        >>> NumberContainer().binary_search_insert(131, 23)\n        Traceback (most recent call last):\n            ...\n        TypeError: binary_search_insert() only accepts either a list, range or str\n        \"\"\"\n        if isinstance(array, (range, str)):\n            array = list(array)\n        elif not isinstance(array, list):\n            raise TypeError(\n                \"binary_search_insert() only accepts either a list, range or str\"\n            )\n\n        low = 0\n        high = len(array) - 1\n\n        while low <= high:\n            mid = (low + high) // 2\n            if array[mid] == index:\n                # If the item already exists in the array,\n                # insert it after the existing item\n                array.insert(mid + 1, index)\n                return array\n            elif array[mid] < index:\n                low = mid + 1\n            else:\n                high = mid - 1\n","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/other/number_container_system.py#L84-L120","documentation":"Raised by NumberContainer.binary_search_insert when the array argument is not a list, range, or str. As with the delete variant, range and str are coerced to list and every other type (int, tuple, dict, None, NumPy array) is rejected with a TypeError before any insertion logic runs.","triggerScenarios":"Calling binary_search_insert(131, 23), binary_search_insert((1,2,3), 2), or passing any non-list/range/str container as the first argument.","commonSituations":"Passing tuples or NumPy arrays from data pipelines, or a scalar where the sorted container was expected.","solutions":["Pass a list (or range/str which get converted): binary_search_insert([0,1,3], 2)","Convert other sequence types first: binary_search_insert(list(tup), value) — the input must already be sorted for correct placement"],"exampleFix":"# before\nNumberContainer().binary_search_insert(131, 23)  # TypeError\n\n# after\nNumberContainer().binary_search_insert([131], 23)","handlingStrategy":"type-guard","validationCode":"def acceptable_array(array) -> bool:\n    return isinstance(array, (list, range, str))","typeGuard":"def is_supported_container(array) -> bool:\n    return isinstance(array, (list, range, str))","tryCatchPattern":null,"preventionTips":["Convert tuples/NumPy arrays to list before insertion calls","Keep the container sorted — insertion placement assumes sorted input"],"tags":["type-validation","binary-search","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}