{"record":{"id":"2965c7904d5e7e83","repo":"TheAlgorithms/Python","slug":"binary-search-delete-only-accepts-either-a-list","errorCode":null,"errorMessage":"binary_search_delete() only accepts either a list, range or str","messagePattern":"binary_search_delete\\(\\) only accepts either a list, range or str","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"other/number_container_system.py","lineNumber":57,"sourceCode":"        >>> NumberContainer().binary_search_delete(\"abcde\", \"c\")\n        ['a', 'b', 'd', 'e']\n        >>> NumberContainer().binary_search_delete([0, -1, 2, 4], 0)\n        Traceback (most recent call last):\n            ...\n        ValueError: Either the item is not in the array or the array was unsorted\n        >>> NumberContainer().binary_search_delete([2, 0, 4, -1, 11], -1)\n        Traceback (most recent call last):\n            ...\n        ValueError: Either the item is not in the array or the array was unsorted\n        >>> NumberContainer().binary_search_delete(125, 1)\n        Traceback (most recent call last):\n            ...\n        TypeError: binary_search_delete() 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_delete() 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] == item:\n                array.pop(mid)\n                return array\n            elif array[mid] < item:\n                low = mid + 1\n            else:\n                high = mid - 1\n        raise ValueError(\n            \"Either the item is not in the array or the array was unsorted\"\n        )","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/other/number_container_system.py#L39-L75","documentation":"Raised by NumberContainer.binary_search_delete when the array argument is not a list, range, or str. Range and str inputs are converted to list first; anything else (int, tuple, dict, None, NumPy array) is rejected with a TypeError before the search loop runs.","triggerScenarios":"Calling binary_search_delete(125, 1), binary_search_delete((1,2,3), 2), or binary_search_delete(None, 0) — any non-list/range/str first argument.","commonSituations":"Passing a tuple or NumPy array from upstream data processing, or an unboxed scalar where a container was expected.","solutions":["Pass a list: binary_search_delete([2, 0, 4, -1, 11], -1)","Convert other sequences at the call site: binary_search_insert(..., list(tuple_or_array)) — note the list must already be sorted for the delete to succeed"],"exampleFix":"# before\nNumberContainer().binary_search_delete(125, 1)  # TypeError\n\n# after\nNumberContainer().binary_search_delete([125], 1)","handlingStrategy":"type-guard","validationCode":"def acceptable_array(array) -> bool:\n    return isinstance(array, (list, range, str))","typeGuard":"from typing import Any\n\ndef is_supported_container(array: Any) -> bool:\n    \"\"\"True when binary_search_delete/insert accept the value.\"\"\"\n    return isinstance(array, (list, range, str))","tryCatchPattern":null,"preventionTips":["Convert tuples and NumPy arrays to list at the boundary of your code","Annotate APIs with the accepted types so mypy flags misuse early"],"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"}