TheAlgorithms/Python · error · ValueError
empty number list not allowed
Error message
empty number list not allowed
What it means
Raised by build_sparse_table() in data_structures/arrays/sparse_table.py when number_list is empty. The sparse table answers range-minimum queries via powers of two (int(log2(length)) rows); with length 0, log2(0) is undefined, so construction is refused up front.
Source
Thrown at data_structures/arrays/sparse_table.py:32
from math import log2
def build_sparse_table(number_list: list[int]) -> list[list[int]]:
"""
Precompute range minimum queries with power of two length and store the precomputed
values in a table.
>>> build_sparse_table([8, 1, 0, 3, 4, 9, 3])
[[8, 1, 0, 3, 4, 9, 3], [1, 0, 0, 3, 4, 3, 0], [0, 0, 0, 3, 0, 0, 0]]
>>> build_sparse_table([3, 1, 9])
[[3, 1, 9], [1, 1, 0]]
>>> build_sparse_table([])
Traceback (most recent call last):
...
ValueError: empty number list not allowed
"""
if not number_list:
raise ValueError("empty number list not allowed")
length = len(number_list)
# Initialise sparse_table -- sparse_table[j][i] represents the minimum value of the
# subset of length (2 ** j) of number_list, starting from index i.
# smallest power of 2 subset length that fully covers number_list
row = int(log2(length)) + 1
sparse_table = [[0 for i in range(length)] for j in range(row)]
# minimum of subset of length 1 is that value itself
for i, value in enumerate(number_list):
sparse_table[0][i] = value
j = 1
# compute the minimum value for all intervals with size (2 ** j)
while (1 << j) <= length:
i = 0
# while subset starting from i still have at least (2 ** j) elementsView on GitHub (pinned to f5988cc097)
Solutions
- Skip table construction for empty input: if data: table = build_sparse_table(data).
- Validate at the data-ingestion boundary so empty datasets fail with a domain-specific error.
- For bucketed RMQ, either skip empty buckets or give them a neutral sentinel before building.
Example fix
# before table = build_sparse_table(values) # values == [] # after table = build_sparse_table(values) if values else None
Defensive patterns
Strategy: validation
Validate before calling
if not values:
raise ValueError('cannot build sparse table for empty data')
table = build_sparse_table(values) Prevention
- Skip RMQ table construction for empty buckets/windows
- Reject empty datasets at ingestion
When it happens
Trigger: Calling build_sparse_table([]), or building the table from data that a filter reduced to nothing.
Common situations: RMQ over sliding windows/buckets where a bucket can be empty, empty input files or query results feeding the table builder, or initializing tables for arrays whose size is determined at runtime and can be zero.
Related errors
- list index out of range
- The parameter s must not be empty.
- The parameter bwt_string must not be empty.
- We need some text to work with.
- The array is empty.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/e4ca95e931e633c4.
Report an issue: GitHub.