{"record":{"id":"050d26dd814efce2","repo":"TheAlgorithms/Python","slug":"invalid-input-edge-is-the-wrong-length","errorCode":null,"errorMessage":"Invalid input: {edge} is the wrong length.","messagePattern":"Invalid input: (.+?) is the wrong length\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/graph_adjacency_list.py","lineNumber":57,"sourceCode":"        - edges: (list[list[T]]) The list of edges the client wants to\n        pass in. Each edge is a 2-element list. Default is empty.\n        - directed: (bool) Indicates if graph is directed or undirected.\n        Default is True.\n        \"\"\"\n        self.adj_list: dict[T, list[T]] = {}  # dictionary of lists of T\n        self.directed = directed\n\n        # Falsey checks\n        edges = edges or []\n        vertices = vertices or []\n\n        for vertex in vertices:\n            self.add_vertex(vertex)\n\n        for edge in edges:\n            if len(edge) != 2:\n                msg = f\"Invalid input: {edge} is the wrong length.\"\n                raise ValueError(msg)\n            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.\"","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_list.py#L39-L75","documentation":"Raised by GraphAdjacencyList.__init__ (graphs/graph_adjacency_list.py:57) when any element of the edges parameter does not have exactly two elements. Edges must be 2-tuples/lists of (source, destination); this is an unweighted adjacency-list graph, so a weight in the edge would also trigger it.","triggerScenarios":"GraphAdjacencyList(edges=[('A','B','C')]) — three elements (e.g. a weighted edge); edges=[('A',)] — one element; edges=[['A','B','C']] from a loader that kept weights. Note the constructor iterates `edges or []`, so edges=0 or edges='' behave like no edges rather than raising.","commonSituations":"Feeding weighted edge lists (u, v, w) from other algorithms into this unweighted graph; heterogeneous data where some rows have extra columns; CSV rows loaded as tuples with trailing fields.","solutions":["Strip weights before constructing: edges = [(u, v) for u, v, *_ in raw_edges]","Validate row lengths in your data loader and reject/repair malformed rows early","Use a weighted graph class if you actually need edge weights"],"exampleFix":"# before\nraw = [(\"A\", \"B\", 5), (\"B\", \"C\", 2)]\ng = GraphAdjacencyList(vertices=[], edges=raw)  # raises: wrong length\n\n# after\nraw = [(\"A\", \"B\", 5), (\"B\", \"C\", 2)]\ng = GraphAdjacencyList(vertices=[], edges=[(u, v) for u, v, _ in raw])","handlingStrategy":"validation","validationCode":"normalized = []\nfor e in edges:\n    if len(e) != 2:\n        raise ValueError(f\"edge must be (source, destination), got {e!r}\")\n    normalized.append((e[0], e[1]))\ng = GraphAdjacencyList(vertices=vertices, edges=normalized)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Strip weights/extra columns when loading edges into this unweighted graph","Keep one canonical loader that enforces the (u, v) shape"],"tags":["graphs","input-validation","constructor"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}