{"record":{"id":"a32dfa9fee17b6c4","repo":"TheAlgorithms/Python","slug":"incorrect-input-vertex-is-already-in-the-graph","errorCode":null,"errorMessage":"Incorrect input: {vertex} is already in the graph.","messagePattern":"Incorrect input: (.+?) is already in the graph\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/graph_adjacency_list.py","lineNumber":76,"sourceCode":"            self.add_edge(edge[0], edge[1])\n\n    def add_vertex(self, vertex: T) -> None:\n        \"\"\"\n        Adds a vertex to the graph. If the given vertex already exists,\n        a ValueError will be thrown.\n\n        >>> g = GraphAdjacencyList(vertices=[], edges=[], directed=False)\n        >>> g.add_vertex(\"A\")\n        >>> g.adj_list\n        {'A': []}\n        >>> g.add_vertex(\"A\")\n        Traceback (most recent call last):\n        ...\n        ValueError: Incorrect input: A is already in the graph.\n        \"\"\"\n        if self.contains_vertex(vertex):\n            msg = f\"Incorrect input: {vertex} is already in the graph.\"\n            raise ValueError(msg)\n        self.adj_list[vertex] = []\n\n    def add_edge(self, source_vertex: T, destination_vertex: T) -> None:\n        \"\"\"\n        Creates an edge from source vertex to destination vertex. If any\n        given vertex doesn't exist or the edge already exists, a ValueError\n        will be thrown.\n        \"\"\"\n        if not (\n            self.contains_vertex(source_vertex)\n            and self.contains_vertex(destination_vertex)\n        ):\n            msg = (\n                f\"Incorrect input: Either {source_vertex} or \"\n                f\"{destination_vertex} does not exist\"\n            )\n            raise ValueError(msg)\n        if self.contains_edge(source_vertex, destination_vertex):","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_list.py#L58-L94","documentation":"Raised by GraphAdjacencyList.add_vertex (graphs/graph_adjacency_list.py:76) when the vertex is already present (contains_vertex returns True). Vertices are unique keys in the adjacency dict, so adding a duplicate is treated as an incorrect-input error rather than an idempotent no-op.","triggerScenarios":"g.add_vertex(\"A\") twice; building a graph from a list that mentions a vertex in several edges, then adding all edge endpoints with add_vertex in a loop; case-sensitive vertex sets where 'a' and 'A' are distinct but data contains near-duplicates.","commonSituations":"Loading vertex lists that contain duplicates; re-running ingestion code against the same graph object; user data where the same entity appears under the same label multiple times.","solutions":["Guard with contains_vertex: if not g.contains_vertex(v): g.add_vertex(v)","Deduplicate the input before constructing: vertices = list(set(vertices)) (order-insensitive) or dict.fromkeys(vertices) to preserve order","Let the constructor register vertices from edges by calling add_edge, which only requires vertices added beforehand — or just catch ValueError where duplicates are benign"],"exampleFix":"# before\nfor v in vertex_list:\n    g.add_vertex(v)  # raises on duplicates\n\n# after\nfor v in dict.fromkeys(vertex_list):  # deduped, order preserved\n    if not g.contains_vertex(v):\n        g.add_vertex(v)","handlingStrategy":"validation","validationCode":"for v in dict.fromkeys(vertices):  # dedupe, keep order\n    if not g.contains_vertex(v):\n        g.add_vertex(v)","typeGuard":null,"tryCatchPattern":"try:\n    g.add_vertex(v)\nexcept ValueError:\n    pass  # already present; treat as idempotent","preventionTips":["Deduplicate vertex lists before ingestion","Make add operations conditional on contains_vertex when data may repeat"],"tags":["graphs","duplicate","vertex"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}