TheAlgorithms/Python · error · ValueError
Incorrect input: {vertex} does not exist in this graph.
Error message
Incorrect input: {vertex} does not exist in this graph. What it means
Raised by GraphAdjacencyList.remove_vertex when the vertex passed is not a key in the internal adjacency dict (checked via contains_vertex, i.e. `vertex in self.adj_list`). The library refuses to remove something that was never added so the dict stays consistent. It is a plain ValueError, so it can be caught with `except ValueError`.
Source
Thrown at graphs/graph_adjacency_list.py:115
f"{source_vertex} and {destination_vertex}"
)
raise ValueError(msg)
# add the destination vertex to the list associated with the source vertex
# and vice versa if not directed
self.adj_list[source_vertex].append(destination_vertex)
if not self.directed:
self.adj_list[destination_vertex].append(source_vertex)
def remove_vertex(self, vertex: T) -> None:
"""
Removes the given vertex from the graph and deletes all incoming and
outgoing edges from the given vertex as well. If the given vertex
does not exist, a ValueError will be thrown.
"""
if not self.contains_vertex(vertex):
msg = f"Incorrect input: {vertex} does not exist in this graph."
raise ValueError(msg)
if not self.directed:
# If not directed, find all neighboring vertices and delete all references
# of edges connecting to the given vertex
for neighbor in self.adj_list[vertex]:
self.adj_list[neighbor].remove(vertex)
else:
# If directed, search all neighbors of all vertices and delete all
# references of edges connecting to the given vertex
for edge_list in self.adj_list.values():
if vertex in edge_list:
edge_list.remove(vertex)
# Finally, delete the given vertex and all of its outgoing edge references
self.adj_list.pop(vertex)
def remove_edge(self, source_vertex: T, destination_vertex: T) -> None:
"""View on GitHub (pinned to f5988cc097)
Solutions
- Check `graph.contains_vertex(vertex)` before calling remove_vertex.
- Audit the calling loop for double-removal or stale vertex references (e.g. iterating a snapshot while mutating).
- Verify the vertex type matches what was used in add_vertex (1 vs '1').
- Wrap the call in try/except ValueError if removal is best-effort (e.g. cleanup paths).
Example fix
// before
graph.remove_vertex(node) # node may already be gone
// after
if graph.contains_vertex(node):
graph.remove_vertex(node) Defensive patterns
Strategy: validation
Validate before calling
if graph.contains_vertex(vertex):
graph.remove_vertex(vertex) Type guard
def can_remove_vertex(graph, vertex) -> bool:
return graph.contains_vertex(vertex) Try / catch
try:
graph.remove_vertex(vertex)
except ValueError as exc:
if "does not exist in this graph" not in str(exc):
raise
logger.debug("vertex already absent: %r", vertex) Prevention
- Maintain a single source of truth for which vertices exist; never re-derive it from stale lists.
- Make removal loops idempotent with contains_vertex guards.
- Use one consistent type for vertex labels (all int or all str) end to end.
When it happens
Trigger: Calling remove_vertex(v) on a graph that never had v added, after v was already removed, or after clear_graph(). Also triggered when the vertex object is equal-but-different in a way the dict does not match (e.g. passing 1 vs '1', or an unhashable type raising before this check).
Common situations: Processing user-supplied node lists where some nodes were filtered out before graph construction; double-removal in loops; calling remove_vertex inside an iteration that already deleted the vertex; confusion between value types (str vs int vertex labels).
Related errors
- Incorrect input: Either {source_vertex} or {destination_vert
- Incorrect input: {vertex} does not exist in this graph.
- Power cannot be negative in any electrical/electronics syste
- One and only one argument must be 0
- One and only one argument must be 0
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/74d648cc54e1f567.
Report an issue: GitHub.