TheAlgorithms/Python · error · ValueError
Node {node_index} should have a list of neighbors.
Error message
Node {node_index} should have a list of neighbors. What it means
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.
Source
Thrown at graphs/lanczos_eigenvectors.py:57
ValueError: If the graph is not a list of lists, or if any node has
invalid neighbors (e.g., out-of-range or non-integer values).
>>> validate_adjacency_list([[1, 2], [0], [0, 1]])
>>> validate_adjacency_list([[]]) # No neighbors, valid case
>>> validate_adjacency_list([[1], [2], [-1]]) # Invalid neighbor
Traceback (most recent call last):
...
ValueError: Invalid neighbor -1 in node 2 adjacency list.
"""
if not isinstance(graph, list):
raise ValueError("Graph should be a list of lists.")
for node_index, neighbors in enumerate(graph):
if not isinstance(neighbors, list):
no_neighbors_message: str = (
f"Node {node_index} should have a list of neighbors."
)
raise ValueError(no_neighbors_message)
for neighbor_index in neighbors:
if (
not isinstance(neighbor_index, int)
or neighbor_index < 0
or neighbor_index >= len(graph)
):
invalid_neighbor_message: str = (
f"Invalid neighbor {neighbor_index} in node {node_index} "
f"adjacency list."
)
raise ValueError(invalid_neighbor_message)
def lanczos_iteration(
graph: list[list[int | None]], num_eigenvectors: int
) -> tuple[np.ndarray, np.ndarray]:
"""Constructs the tridiagonal matrix and orthonormal basis vectors using the
Lanczos method.View on GitHub (pinned to f5988cc097)
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).
Example fix
# before graph = [1, 2, 0] # flat, raises at node 0 # after graph = [[1, 2], [0], [0]] # per-node neighbor lists
Defensive patterns
Strategy: validation
Validate before calling
graph = [row if isinstance(row, list) else [] for row in graph]
Type guard
def rows_are_lists(graph) -> bool:
return isinstance(graph, list) and all(isinstance(row, list) for row in graph) Prevention
- 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).
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Graph should be a list of lists.
- Incorrect input: {vertex} does not exist in this graph.
- Incorrect input: The edge does NOT exist between {source_ver
- Incorrect input: Either {source_vertex} or {destination_vert
- Invalid neighbor {neighbor_index} in node {node_index} adjac
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/f391f7decbe3d66c.
Report an issue: GitHub.