TheAlgorithms/Python · error · ValueError
Incorrect input: {vertex} is already in the graph.
Error message
Incorrect input: {vertex} is already in the graph. What it means
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.
Source
Thrown at graphs/graph_adjacency_list.py:76
self.add_edge(edge[0], edge[1])
def add_vertex(self, vertex: T) -> None:
"""
Adds a vertex to the graph. If the given vertex already exists,
a ValueError will be thrown.
>>> g = GraphAdjacencyList(vertices=[], edges=[], directed=False)
>>> g.add_vertex("A")
>>> g.adj_list
{'A': []}
>>> g.add_vertex("A")
Traceback (most recent call last):
...
ValueError: Incorrect input: A is already in the graph.
"""
if self.contains_vertex(vertex):
msg = f"Incorrect input: {vertex} is already in the graph."
raise ValueError(msg)
self.adj_list[vertex] = []
def add_edge(self, source_vertex: T, destination_vertex: T) -> None:
"""
Creates an edge from source vertex to destination vertex. If any
given vertex doesn't exist or the edge already exists, a ValueError
will be thrown.
"""
if not (
self.contains_vertex(source_vertex)
and self.contains_vertex(destination_vertex)
):
msg = (
f"Incorrect input: Either {source_vertex} or "
f"{destination_vertex} does not exist"
)
raise ValueError(msg)
if self.contains_edge(source_vertex, destination_vertex):View on GitHub (pinned to f5988cc097)
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
Example fix
# before
for v in vertex_list:
g.add_vertex(v) # raises on duplicates
# after
for v in dict.fromkeys(vertex_list): # deduped, order preserved
if not g.contains_vertex(v):
g.add_vertex(v) Defensive patterns
Strategy: validation
Validate before calling
for v in dict.fromkeys(vertices): # dedupe, keep order
if not g.contains_vertex(v):
g.add_vertex(v) Try / catch
try:
g.add_vertex(v)
except ValueError:
pass # already present; treat as idempotent Prevention
- Deduplicate vertex lists before ingestion
- Make add operations conditional on contains_vertex when data may repeat
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Incorrect input: Either {source_vertex} or {destination_vert
- Incorrect input: The edge already exists between {source_ver
- Duplicate symbol ({i})
- Algorithm is unable to find solution
- Negative cycle found
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/a32dfa9fee17b6c4.
Report an issue: GitHub.