TheAlgorithms/Python · error · ValueError

Row must be equal in length to the other rows in the matrix

Error message

Row must be equal in length to the other rows in the matrix

What it means

Error "Row must be equal in length to the other rows in the matrix" thrown in TheAlgorithms/Python.

Source

Thrown at matrix/matrix_class.py:257

            + "\n ".join(
                [
                    "[" + ". ".join([str(value) for value in row]) + ".]"
                    for row in self.rows
                ]
            )
            + "]"
        )

    # MATRIX MANIPULATION
    def add_row(self, row: list[int], position: int | None = None) -> None:
        type_error = TypeError("Row must be a list containing all ints and/or floats")
        if not isinstance(row, list):
            raise type_error
        for value in row:
            if not isinstance(value, (int, float)):
                raise type_error
        if len(row) != self.num_columns:
            raise ValueError(
                "Row must be equal in length to the other rows in the matrix"
            )
        if position is None:
            self.rows.append(row)
        else:
            self.rows = [*self.rows[0:position], row, *self.rows[position:]]

    def add_column(self, column: list[int], position: int | None = None) -> None:
        type_error = TypeError(
            "Column must be a list containing all ints and/or floats"
        )
        if not isinstance(column, list):
            raise type_error
        for value in column:
            if not isinstance(value, (int, float)):
                raise type_error
        if len(column) != self.num_rows:
            raise ValueError(

View on GitHub (pinned to f5988cc097)

Solutions

  1. Set the row to the same length as the existing rows of the matrix.

When it happens

Trigger: Thrown at matrix/matrix_class.py:257 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/d9559dd0c0958afd. Report an issue: GitHub.