Keras AttributeError 'list' object has no attribute 'ndim'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error AttributeError: 'list' object has no attribute 'ndim' usually means Keras expected an array-like object with shape metadata, but received a plain Python list instead. The fix is often simple: convert your input or labels to NumPy arrays or tensors, and make sure their shapes match what the model expects.
Why Keras Cares About ndim
Keras needs to know how many dimensions an input has so it can validate batch shape, feature shape, and layer compatibility. NumPy arrays and tensors expose that information through attributes such as shape and ndim. Plain Python lists do not.
That is why code like this often fails:
Depending on the code path and backend, Keras may try to inspect dimensionality and hit a list object that does not provide ndim.
Convert Inputs to Arrays
The most reliable fix is to convert training data to NumPy arrays before passing it to fit, evaluate, or predict.
Keras documentation for training APIs expects array-like or tensor-like input, not arbitrary nested Python structures.
Check for Ragged or Inconsistent Lists
Another common problem is that the list itself is irregular:
Even if you convert that to np.array, NumPy may create an object array instead of a numeric matrix because the rows are not the same length. Keras will then fail later with a different shape or dtype error.
So do two checks:
- convert the data to
np.array - inspect
x.shapeandx.dtype
If the shape is not what you expect, fix the data before training.
Lists Are Valid Only in Certain Cases
A plain list is not always wrong. Multi-input Keras models can accept a list of arrays or a list of tensors, because each item represents a separate model input. The key difference is that each element of that outer list should still be an array-like object, not a raw scalar list in an unexpected format.
For example, this is valid for a two-input model:
The outer container is a list, but the actual inputs are arrays with defined dimensionality.
Debugging Checklist
Before calling fit, print these values:
If you see plain list objects where you expected arrays, convert them. If you see arrays with unexpected shapes, fix the upstream preprocessing step.
This quick inspection is often faster than reading a long stack trace.
Common Pitfalls
Passing Python lists directly into Keras works in some simple cases but is less predictable than using explicit NumPy arrays or tensors.
Converting an irregular nested list to np.array can produce an object array rather than a numeric tensor, which leads to new errors.
Labels can cause the same problem as features. Convert both x and y when training.
For multi-input models, each input should be its own array. A single list of mixed structures is not the same thing.
Debugging only the model and not the data pipeline wastes time because this error is usually about input structure, not layer math.
Summary
- The error appears when Keras expects an array-like object with
ndimbut receives a plain list. - Convert features and labels to NumPy arrays or tensors before training.
- Inspect
shapeanddtypeto catch ragged or malformed data. - Lists are valid for multi-input models only when each element is itself an array-like input.
- Treat this as a data-format problem first, not a model-architecture problem.

