TheAlgorithms/Python · error · TypeError

binary_search_delete() only accepts either a list, range or

Error message

binary_search_delete() only accepts either a list, range or str

What it means

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.

Source

Thrown at other/number_container_system.py:57

        >>> NumberContainer().binary_search_delete("abcde", "c")
        ['a', 'b', 'd', 'e']
        >>> NumberContainer().binary_search_delete([0, -1, 2, 4], 0)
        Traceback (most recent call last):
            ...
        ValueError: Either the item is not in the array or the array was unsorted
        >>> NumberContainer().binary_search_delete([2, 0, 4, -1, 11], -1)
        Traceback (most recent call last):
            ...
        ValueError: Either the item is not in the array or the array was unsorted
        >>> NumberContainer().binary_search_delete(125, 1)
        Traceback (most recent call last):
            ...
        TypeError: binary_search_delete() only accepts either a list, range or str
        """
        if isinstance(array, (range, str)):
            array = list(array)
        elif not isinstance(array, list):
            raise TypeError(
                "binary_search_delete() only accepts either a list, range or str"
            )

        low = 0
        high = len(array) - 1

        while low <= high:
            mid = (low + high) // 2
            if array[mid] == item:
                array.pop(mid)
                return array
            elif array[mid] < item:
                low = mid + 1
            else:
                high = mid - 1
        raise ValueError(
            "Either the item is not in the array or the array was unsorted"
        )

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass a list: binary_search_delete([2, 0, 4, -1, 11], -1)
  2. 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

Example fix

# before
NumberContainer().binary_search_delete(125, 1)  # TypeError

# after
NumberContainer().binary_search_delete([125], 1)
Defensive patterns

Strategy: type-guard

Validate before calling

def acceptable_array(array) -> bool:
    return isinstance(array, (list, range, str))

Type guard

from typing import Any

def is_supported_container(array: Any) -> bool:
    """True when binary_search_delete/insert accept the value."""
    return isinstance(array, (list, range, str))

Prevention

When it happens

Trigger: 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.

Common situations: Passing a tuple or NumPy array from upstream data processing, or an unboxed scalar where a container was expected.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/2965c7904d5e7e83. Report an issue: GitHub.