donnemartin/interactive-coding-challenges · error · TypeError
data cannot be None
Error message
data cannot be None
What it means
SelectionSort.sort raises TypeError('data cannot be None') as the first statement; the algorithm then indexes data[i] and compares elements, so None input is rejected up front with a clear message. Empty and single-element lists are valid and returned unchanged.
Source
Thrown at sorting_searching/selection_sort/selection_sort_solution.ipynb:105
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class SelectionSort(object):\n",
"\n",
" def sort(self, data):\n",
" if data is None:\n",
" raise TypeError('data cannot be None')\n",
" if len(data) < 2:\n",
" return data\n",
" for i in range(len(data) - 1):\n",
" min_index = i\n",
" for j in range(i + 1, len(data)):\n",
" if data[j] < data[min_index]:\n",
" min_index = j\n",
" if data[min_index] < data[i]:\n",
" data[i], data[min_index] = data[min_index], data[i]\n",
" return data\n",
"\n",
" def sort_iterative_alt(self, data):\n",
" if data is None:\n",
" raise TypeError('data cannot be None')\n",
" if len(data) < 2:\n",
" return data\n",
" for i in range(len(data) - 1):\n",
" self._swap(data, i, self._find_min_index(data, i))\n",View on GitHub (pinned to 358f2cc604)
Solutions
- Initialize the variable to [] or guard with 'if data is not None' before sorting
- Fix the upstream producer so it returns a list (or raises its own clear error) instead of None
- Substitute an empty list when None means no data: sort(data or [])
Example fix
// before result = SelectionSort().sort(data) # data may be None // after result = SelectionSort().sort(data or [])
Defensive patterns
Strategy: validation
Validate before calling
if data is None:
data = []
result = SelectionSort().sort(data) Type guard
def is_sortable(x):
return isinstance(x, list) Try / catch
try:
result = ss.sort(data)
except TypeError as e:
if 'cannot be None' in str(e):
result = []
else:
raise Prevention
- Never let data variables stay None before sort calls
- Use 'data or []' normalization after optional loads
- Validate inputs once at the pipeline entry point
When it happens
Trigger: Calling SelectionSort().sort(None); passing an unassigned or failed-to-load list variable.
Common situations: Upstream data loaders returning None on error; optional parameters defaulting to None; notebook cells run out of order so the data variable is still None.
Related errors
- array cannot be None
- sentence cannot be None
- rows and cols cannot be None
- s or t cannot be None
- a or b cannot be None
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/c04028f7ed52fc35.
Report an issue: GitHub.