TheAlgorithms/Python · error · ValueError

The citations should be a list of non negative integers.

Error message

The citations should be a list of non negative integers.

What it means

Raised by h_index when the citations argument is not a list, or when any element is not a non-negative int. The function sorts citations in place and scans them, so it requires a homogeneous list of ints >= 0 (bools pass isinstance(int) but negative or float values do not).

Source

Thrown at other/h_index.py:56

    >>> h_index('test')
    Traceback (most recent call last):
        ...
    ValueError: The citations should be a list of non negative integers.
    >>> h_index([1,2,'3'])
    Traceback (most recent call last):
        ...
    ValueError: The citations should be a list of non negative integers.
    >>> h_index([1,2,-3])
    Traceback (most recent call last):
        ...
    ValueError: The citations should be a list of non negative integers.
    """

    # validate:
    if not isinstance(citations, list) or not all(
        isinstance(item, int) and item >= 0 for item in citations
    ):
        raise ValueError("The citations should be a list of non negative integers.")

    citations.sort()
    len_citations = len(citations)

    for i in range(len_citations):
        if citations[len_citations - 1 - i] <= i:
            return i

    return len_citations


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Coerce and validate before calling: citations = [int(c) for c in citations] and reject negatives
  2. Convert tuples/arrays to a list: h_index(list(citations))
  3. Filter out invalid records at ingestion rather than inside the analysis call

Example fix

# before
h_index([10, 5, -1])  # ValueError: negative citation count

# after
h_index([c for c in [10, 5, -1] if isinstance(c, int) and c >= 0])
Defensive patterns

Strategy: type-guard

Validate before calling

def normalize_citations(citations):
    if not isinstance(citations, list):
        citations = list(citations)
    if not all(isinstance(c, int) and c >= 0 for c in citations):
        raise ValueError('citations must be non-negative integers')
    return citations

Type guard

def is_valid_citations(citations) -> bool:
    return isinstance(citations, list) and all(
        isinstance(c, int) and c >= 0 for c in citations
    )

Try / catch

try:
    h = h_index(citations)
except ValueError as e:
    if 'non negative integers' in str(e):
        citations = [int(c) for c in citations if c is not None]
        h = h_index(citations) if is_valid_citations(citations) else 0
    else:
        raise

Prevention

When it happens

Trigger: Calling h_index([1, 2, -3]) (negative entry), h_index([1.5, 2]) (float entries), h_index('123') or h_index((1, 2)) (not a list), or h_index(None).

Common situations: Feeding bibliometric data scraped from APIs that returns floats or strings, or passing a tuple/NumPy array produced by upstream processing instead of a plain list.

Related errors


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