TheAlgorithms/Python · error · ValueError
Either the item is not in the array or the array was unsorte
Error message
Either the item is not in the array or the array was unsorted
What it means
Raised by NumberContainer.binary_search_delete after the search loop terminates without finding the item. Because binary search only visits the positions a sorted array would place the item, this fires both when the item is genuinely absent and when the array is not sorted — the message intentionally covers both causes.
Source
Thrown at other/number_container_system.py:73
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"
)
def binary_search_insert(self, array: list | str | range, index: int) -> list[int]:
"""
Inserts the index into the sorted array
at the correct position.
>>> NumberContainer().binary_search_insert([1,2,3], 2)
[1, 2, 2, 3]
>>> 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]View on GitHub (pinned to f5988cc097)
Solutions
- Sort the array before deleting: array.sort() then binary_search_delete(array, item)
- Use binary_search_insert to maintain sorted order incrementally instead of plain append
- If absence is expected, wrap the call in try/except ValueError or check `item in array` first (linear but correct for unsorted data)
Example fix
# before NumberContainer().binary_search_delete([2, 0, 4, -1, 11], -1) # unsorted -> ValueError # after arr = sorted([2, 0, 4, -1, 11]) NumberContainer().binary_search_delete(arr, -1)
Defensive patterns
Strategy: validation
Validate before calling
def can_binary_delete(array: list, item) -> bool:
return array == sorted(array) and item in array # cheap guard; O(n log n) Type guard
def is_sorted(array: list) -> bool:
return all(array[i] <= array[i + 1] for i in range(len(array) - 1)) Try / catch
try:
array = container.binary_search_delete(array, item)
except ValueError as e:
if 'not in the array' in str(e):
pass # absent item: nothing to delete
else:
raise Prevention
- Maintain sorted order with binary_search_insert; never append raw values
- Sort once at ingestion and assert is_sorted before binary operations
When it happens
Trigger: Calling binary_search_delete([2, 0, 4, -1, 11], -1) (unsorted input — the doctest itself shows this failing), or searching for a value that does not exist in an otherwise sorted array.
Common situations: Forgetting that the container assumes sorted order (e.g. appending values without binary_search_insert), or deleting an item already removed by an earlier operation.
Related errors
- sorted_collection must be sorted in ascending order
- The parameter s must not be empty.
- The parameter bwt_string must not be empty.
- The parameter idx_original_string must not be lower than 0.
- The parameter idx_original_string must be lower than len(bwt
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/68908486622678cf.
Report an issue: GitHub.