TheAlgorithms/Python · error · TypeError
Expected a list of numbers as input, found {type(item).__nam
Error message
Expected a list of numbers as input, found {type(item).__name__} What it means
Raised by _validate_point() in maths/manhattan_distance.py when an element inside the point list is not an int or float. The validator iterates each item and raises TypeError with the offending item's type name embedded in the message (e.g. 'found str'). It is called by manhattan_distance() and manhattan_distance_one_liner() on both points.
Source
Thrown at maths/manhattan_distance.py:73
TypeError: Expected a list of numbers as input, found str
>>> _validate_point(1)
Traceback (most recent call last):
...
TypeError: Expected a list of numbers as input, found int
>>> _validate_point("not_a_list")
Traceback (most recent call last):
...
TypeError: Expected a list of numbers as input, found str
"""
if point:
if isinstance(point, list):
for item in point:
if not isinstance(item, (int, float)):
msg = (
"Expected a list of numbers as input, found "
f"{type(item).__name__}"
)
raise TypeError(msg)
else:
msg = f"Expected a list of numbers as input, found {type(point).__name__}"
raise TypeError(msg)
else:
raise ValueError("Missing an input")
def manhattan_distance_one_liner(point_a: list, point_b: list) -> float:
"""
Version with one liner
>>> manhattan_distance_one_liner([1,1], [2,2])
2.0
>>> manhattan_distance_one_liner([1.5,1.5], [2,2])
1.0
>>> manhattan_distance_one_liner([1.5,1.5], [2.5,2])
1.5
>>> manhattan_distance_one_liner([-3, -3, -3], [0, 0, 0])View on GitHub (pinned to f5988cc097)
Solutions
- Coerce the whole list before calling: [float(x) for x in point].
- Strip or impute None/missing values during preprocessing instead of leaving them in the vector.
- When reading CSVs, use converters or dtype so numeric columns parse as numbers.
Example fix
# before manhattan_distance([1, '2'], [3, 4]) # after manhattan_distance([1, 2], [3, 4]) # or [float(x) for x in point_a]
Defensive patterns
Strategy: validation
Validate before calling
point_a = [float(x) for x in point_a] point_b = [float(x) for x in point_b]
Type guard
def is_numeric_list(p) -> bool:
return isinstance(p, list) and all(isinstance(x, (int, float)) and not isinstance(x, bool) for x in p) Prevention
- Coerce numeric columns with pd.to_numeric or float() at load time.
- Impute or drop missing values before building distance vectors.
When it happens
Trigger: manhattan_distance([1,'two'], [1,2]) raises 'found str'; manhattan_distance([1,None], [1,2]) raises 'found NoneType'; manhattan_distance([1, 1], [2, True]) passes since bool subclasses int.
Common situations: Vectors parsed from CSV/JSON where numbers stay strings, lists containing None for missing data, or mixed-type columns not coerced with pd.to_numeric.
Related errors
- Expected a list of numbers as input, found {type(point).__na
- Input must be an integer
- Input value must be a positive integer
- Input value must be a 'int' type
- Input must be an integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/aab02e4bde7833b4.
Report an issue: GitHub.