TheAlgorithms/Python · error · TypeError
Unsupported type given for another ({type(another)})
Error message
Unsupported type given for another ({type(another)}) What it means
Raised by the local Matrix class's multiplication method in sherman_morrison when the right operand is neither a number nor a Matrix instance. This standalone Matrix class (used for the Sherman-Morrison inverse-update algorithm) only supports scalar multiplication and matrix multiplication; anything else — strings, nested lists, numpy arrays — is rejected with a TypeError naming the offending type.
Source
Thrown at matrix/sherman_morrison.py:178
"""
if isinstance(another, (int, float)): # Scalar multiplication
result = Matrix(self.row, self.column)
for r in range(self.row):
for c in range(self.column):
result[r, c] = self[r, c] * another
return result
elif isinstance(another, Matrix): # Matrix multiplication
assert self.column == another.row
result = Matrix(self.row, another.column)
for r in range(self.row):
for c in range(another.column):
for i in range(self.column):
result[r, c] += self[r, i] * another[i, c]
return result
else:
msg = f"Unsupported type given for another ({type(another)})"
raise TypeError(msg)
def transpose(self) -> Matrix:
"""
<method Matrix.transpose>
Return self^T.
Example:
>>> a = Matrix(2, 3)
>>> for r in range(2):
... for c in range(3):
... a[r,c] = r*c
...
>>> a.transpose()
Matrix consist of 3 rows and 2 columns
[0, 0]
[0, 1]
[0, 2]
"""
View on GitHub (pinned to f5988cc097)
Solutions
- Read the type in the error message to identify the culprit operand.
- Wrap nested lists in this module's Matrix class before multiplying.
- Convert NumPy arrays to plain numbers or to this Matrix type; or move the whole computation into NumPy.
- Check for None operands (failed initialization/lookup) before the multiply.
Example fix
# before result = a * [[1, 0], [0, 1]] # TypeError: got <class 'list'> # after result = a * Matrix(2, 2, [[1, 0], [0, 1]]) # or however the class constructor takes data
Defensive patterns
Strategy: type-guard
Validate before calling
def as_sherman_matrix(x, MatrixCls):
"""Wrap nested lists in this module's Matrix; pass numbers and Matrix through."""
if isinstance(x, (int, float)) or isinstance(x, MatrixCls):
return x
if isinstance(x, (list, tuple)):
return MatrixCls(len(x), len(x[0]), x)
raise TypeError(f"unsupported operand {type(x).__name__}")
result = a * as_sherman_matrix(other, Matrix) Type guard
def is_mul_operand(x, MatrixCls) -> bool:
"""Guard: number or this module's Matrix instance."""
return isinstance(x, (int, float)) or isinstance(x, MatrixCls) Try / catch
try:
result = a * operand
except TypeError as e:
if "Unsupported type given for another" in str(e):
# message names the actual type; fix the operand at its source
raise TypeError(f"bad operand came from upstream: {operand!r}") from e
raise Prevention
- Wrap raw nested-list fixtures in this module's Matrix class in tests.
- Keep NumPy arrays out of expressions involving this class, or convert the whole computation to NumPy.
- Guard against None (failed lookups) before multiplying — its type shows up in this error message too.
When it happens
Trigger: sherman_morrison_matrix * [[1, 0], [0, 1]] (raw list), matrix * np.array(...), or matrix * None (a variable that failed to initialize). The f-string in the message tells you exactly which type arrived, e.g. 'Unsupported type given for another (<class \"list\">)'.
Common situations: Interoperating with NumPy arrays; passing unwrapped nested-list test fixtures; None leaking in from a failed lookup of a second matrix; feeding data straight from JSON.
Related errors
- A Matrix can only be multiplied by an int, float, or another
- A Matrix can only be raised to the power of an int
- Step size must be an integer.
- Expected a matrix, got int/list instead
- The input value of 'num_rows' should be 'int'
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/eb20a23917a43563.
Report an issue: GitHub.