{"record":{"id":"b6d0cd116e1cff13","repo":"TheAlgorithms/Python","slug":"the-citations-should-be-a-list-of-non-negative-int","errorCode":null,"errorMessage":"The citations should be a list of non negative integers.","messagePattern":"The citations should be a list of non negative integers\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"other/h_index.py","lineNumber":56,"sourceCode":"    >>> h_index('test')\n    Traceback (most recent call last):\n        ...\n    ValueError: The citations should be a list of non negative integers.\n    >>> h_index([1,2,'3'])\n    Traceback (most recent call last):\n        ...\n    ValueError: The citations should be a list of non negative integers.\n    >>> h_index([1,2,-3])\n    Traceback (most recent call last):\n        ...\n    ValueError: The citations should be a list of non negative integers.\n    \"\"\"\n\n    # validate:\n    if not isinstance(citations, list) or not all(\n        isinstance(item, int) and item >= 0 for item in citations\n    ):\n        raise ValueError(\"The citations should be a list of non negative integers.\")\n\n    citations.sort()\n    len_citations = len(citations)\n\n    for i in range(len_citations):\n        if citations[len_citations - 1 - i] <= i:\n            return i\n\n    return len_citations\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":38,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/other/h_index.py#L38-L72","documentation":"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).","triggerScenarios":"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).","commonSituations":"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.","solutions":["Coerce and validate before calling: citations = [int(c) for c in citations] and reject negatives","Convert tuples/arrays to a list: h_index(list(citations))","Filter out invalid records at ingestion rather than inside the analysis call"],"exampleFix":"# before\nh_index([10, 5, -1])  # ValueError: negative citation count\n\n# after\nh_index([c for c in [10, 5, -1] if isinstance(c, int) and c >= 0])","handlingStrategy":"type-guard","validationCode":"def normalize_citations(citations):\n    if not isinstance(citations, list):\n        citations = list(citations)\n    if not all(isinstance(c, int) and c >= 0 for c in citations):\n        raise ValueError('citations must be non-negative integers')\n    return citations","typeGuard":"def is_valid_citations(citations) -> bool:\n    return isinstance(citations, list) and all(\n        isinstance(c, int) and c >= 0 for c in citations\n    )","tryCatchPattern":"try:\n    h = h_index(citations)\nexcept ValueError as e:\n    if 'non negative integers' in str(e):\n        citations = [int(c) for c in citations if c is not None]\n        h = h_index(citations) if is_valid_citations(citations) else 0\n    else:\n        raise","preventionTips":["Sanitize API-scraped data (strings, floats, None) into non-negative ints at ingestion","Wrap lists/arrays/tuples with list() before calling"],"tags":["input-validation","bibliometrics","argument-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}