{"record":{"id":"f391f7decbe3d66c","repo":"TheAlgorithms/Python","slug":"node-node-index-should-have-a-list-of-neighbors","errorCode":null,"errorMessage":"Node {node_index} should have a list of neighbors.","messagePattern":"Node (.+?) should have a list of neighbors\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/lanczos_eigenvectors.py","lineNumber":57,"sourceCode":"        ValueError: If the graph is not a list of lists, or if any node has\n                    invalid neighbors (e.g., out-of-range or non-integer values).\n\n    >>> validate_adjacency_list([[1, 2], [0], [0, 1]])\n    >>> validate_adjacency_list([[]])  # No neighbors, valid case\n    >>> validate_adjacency_list([[1], [2], [-1]])  # Invalid neighbor\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid neighbor -1 in node 2 adjacency list.\n    \"\"\"\n    if not isinstance(graph, list):\n        raise ValueError(\"Graph should be a list of lists.\")\n\n    for node_index, neighbors in enumerate(graph):\n        if not isinstance(neighbors, list):\n            no_neighbors_message: str = (\n                f\"Node {node_index} should have a list of neighbors.\"\n            )\n            raise ValueError(no_neighbors_message)\n        for neighbor_index in neighbors:\n            if (\n                not isinstance(neighbor_index, int)\n                or neighbor_index < 0\n                or neighbor_index >= len(graph)\n            ):\n                invalid_neighbor_message: str = (\n                    f\"Invalid neighbor {neighbor_index} in node {node_index} \"\n                    f\"adjacency list.\"\n                )\n                raise ValueError(invalid_neighbor_message)\n\n\ndef lanczos_iteration(\n    graph: list[list[int | None]], num_eigenvectors: int\n) -> tuple[np.ndarray, np.ndarray]:\n    \"\"\"Constructs the tridiagonal matrix and orthonormal basis vectors using the\n    Lanczos method.","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/lanczos_eigenvectors.py#L39-L75","documentation":"Raised by validate_adjacency_list in graphs/lanczos_eigenvectors.py when the outer object is a list but the element at position node_index is not itself a list of neighbors. Each node must contribute a list (possibly empty) of neighbor indices; a non-list element (int, None, tuple, string) is rejected with ValueError naming the offending index.","triggerScenarios":"Passing a flat list of neighbor indices instead of nested lists (e.g. [1, 2, 0]); a row set to None or a tuple; ragged data where one node's entry is a bare int; mixing a numpy row into an otherwise-Python list.","commonSituations":"Building the adjacency structure node-by-node and forgetting to wrap single neighbors in a list; JSON data where empty neighbor entries deserialize as null; converting only part of a numpy matrix to lists.","solutions":["Wrap each node's neighbors in a list: `graph = [n if isinstance(n, list) else [n] for n in graph]` only when semantically correct — better, fix construction to always emit lists.","Normalize None rows to empty lists: `graph = [row if row is not None else [] for row in graph]`.","Validate shape before calling: assert all(isinstance(row, list) for row in graph)."],"exampleFix":"# before\ngraph = [1, 2, 0]  # flat, raises at node 0\n\n# after\ngraph = [[1, 2], [0], [0]]  # per-node neighbor lists","handlingStrategy":"validation","validationCode":"graph = [row if isinstance(row, list) else [] for row in graph]","typeGuard":"def rows_are_lists(graph) -> bool:\n    return isinstance(graph, list) and all(isinstance(row, list) for row in graph)","tryCatchPattern":null,"preventionTips":["Always emit per-node neighbor lists, even for a single neighbor or none.","Map null/None entries from JSON to empty lists at load time.","Assert structure before calling: all(isinstance(r, list) for r in graph)."],"tags":["graph","lanczos","adjacency-list","structure-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}