{"record":{"id":"74d648cc54e1f567","repo":"TheAlgorithms/Python","slug":"incorrect-input-vertex-does-not-exist-in-this-g","errorCode":null,"errorMessage":"Incorrect input: {vertex} does not exist in this graph.","messagePattern":"Incorrect input: (.+?) does not exist in this graph\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/graph_adjacency_list.py","lineNumber":115,"sourceCode":"                f\"{source_vertex} and {destination_vertex}\"\n            )\n            raise ValueError(msg)\n\n        # add the destination vertex to the list associated with the source vertex\n        # and vice versa if not directed\n        self.adj_list[source_vertex].append(destination_vertex)\n        if not self.directed:\n            self.adj_list[destination_vertex].append(source_vertex)\n\n    def remove_vertex(self, vertex: T) -> None:\n        \"\"\"\n        Removes the given vertex from the graph and deletes all incoming and\n        outgoing edges from the given vertex as well. If the given vertex\n        does not exist, a ValueError will be thrown.\n        \"\"\"\n        if not self.contains_vertex(vertex):\n            msg = f\"Incorrect input: {vertex} does not exist in this graph.\"\n            raise ValueError(msg)\n\n        if not self.directed:\n            # If not directed, find all neighboring vertices and delete all references\n            # of edges connecting to the given vertex\n            for neighbor in self.adj_list[vertex]:\n                self.adj_list[neighbor].remove(vertex)\n        else:\n            # If directed, search all neighbors of all vertices and delete all\n            # references of edges connecting to the given vertex\n            for edge_list in self.adj_list.values():\n                if vertex in edge_list:\n                    edge_list.remove(vertex)\n\n        # Finally, delete the given vertex and all of its outgoing edge references\n        self.adj_list.pop(vertex)\n\n    def remove_edge(self, source_vertex: T, destination_vertex: T) -> None:\n        \"\"\"","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_list.py#L97-L133","documentation":"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`.","triggerScenarios":"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).","commonSituations":"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).","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)."],"exampleFix":"// before\ngraph.remove_vertex(node)  # node may already be gone\n\n// after\nif graph.contains_vertex(node):\n    graph.remove_vertex(node)","handlingStrategy":"validation","validationCode":"if graph.contains_vertex(vertex):\n    graph.remove_vertex(vertex)","typeGuard":"def can_remove_vertex(graph, vertex) -> bool:\n    return graph.contains_vertex(vertex)","tryCatchPattern":"try:\n    graph.remove_vertex(vertex)\nexcept ValueError as exc:\n    if \"does not exist in this graph\" not in str(exc):\n        raise\n    logger.debug(\"vertex already absent: %r\", vertex)","preventionTips":["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."],"tags":["graph","adjacency-list","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}