TheAlgorithms/Python · error · ValueError

Column must be equal in length to the other columns in the m

Error message

Column must be equal in length to the other columns in the matrix

What it means

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

Source

Thrown at matrix/matrix_class.py:275

            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(
                "Column must be equal in length to the other columns in the matrix"
            )
        if position is None:
            self.rows = [self.rows[i] + [column[i]] for i in range(self.num_rows)]
        else:
            self.rows = [
                [*self.rows[i][0:position], column[i], *self.rows[i][position:]]
                for i in range(self.num_rows)
            ]

    # MATRIX OPERATIONS
    def __eq__(self, other: object) -> bool:
        if not isinstance(other, Matrix):
            return NotImplemented
        return self.rows == other.rows

    def __ne__(self, other: object) -> bool:
        return not self == other

View on GitHub (pinned to f5988cc097)

Solutions

  1. Set the column to the same length as the existing columns of the matrix.

When it happens

Trigger: Thrown at matrix/matrix_class.py:275 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/63006662adcb8f98. Report an issue: GitHub.