TheAlgorithms/Python · error · TypeError

binary_search_insert() only accepts either a list, range or

Error message

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

What it means

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.

Source

Thrown at other/number_container_system.py:102

        >>> NumberContainer().binary_search_insert([0,1,3], 2)
        [0, 1, 2, 3]
        >>> NumberContainer().binary_search_insert([-5, -3, 0, 0, 11, 103], 51)
        [-5, -3, 0, 0, 11, 51, 103]
        >>> NumberContainer().binary_search_insert([-5, -3, 0, 0, 11, 100, 103], 101)
        [-5, -3, 0, 0, 11, 100, 101, 103]
        >>> NumberContainer().binary_search_insert(range(10), 4)
        [0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9]
        >>> NumberContainer().binary_search_insert("abd", "c")
        ['a', 'b', 'c', 'd']
        >>> NumberContainer().binary_search_insert(131, 23)
        Traceback (most recent call last):
            ...
        TypeError: binary_search_insert() 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_insert() only accepts either a list, range or str"
            )

        low = 0
        high = len(array) - 1

        while low <= high:
            mid = (low + high) // 2
            if array[mid] == index:
                # If the item already exists in the array,
                # insert it after the existing item
                array.insert(mid + 1, index)
                return array
            elif array[mid] < index:
                low = mid + 1
            else:
                high = mid - 1

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass a list (or range/str which get converted): binary_search_insert([0,1,3], 2)
  2. Convert other sequence types first: binary_search_insert(list(tup), value) — the input must already be sorted for correct placement

Example fix

# before
NumberContainer().binary_search_insert(131, 23)  # TypeError

# after
NumberContainer().binary_search_insert([131], 23)
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Prevention

When it happens

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

Common situations: Passing tuples or NumPy arrays from data pipelines, or a scalar where the sorted container was expected.

Related errors


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