{"record":{"id":"65242cad372ce84a","repo":"TheAlgorithms/Python","slug":"var-name-must-be-a-list","errorCode":null,"errorMessage":"{var_name} must be a list","messagePattern":"(.+?) must be a list","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/viterbi.py","lineNumber":278,"sourceCode":"    _validate_list(observations_space, \"observations_space\")\n    _validate_list(states_space, \"states_space\")\n\n\ndef _validate_list(_object: Any, var_name: str) -> None:\n    \"\"\"\n    >>> _validate_list([\"a\"], \"mock_name\")\n    >>> _validate_list(\"a\", \"mock_name\")\n    Traceback (most recent call last):\n            ...\n    ValueError: mock_name must be a list\n    >>> _validate_list([0.5], \"mock_name\")\n    Traceback (most recent call last):\n            ...\n    ValueError: mock_name must be a list of strings\n    \"\"\"\n    if not isinstance(_object, list):\n        msg = f\"{var_name} must be a list\"\n        raise ValueError(msg)\n    else:\n        for x in _object:\n            if not isinstance(x, str):\n                msg = f\"{var_name} must be a list of strings\"\n                raise ValueError(msg)\n\n\ndef _validate_dicts(\n    initial_probabilities: Any,\n    transition_probabilities: Any,\n    emission_probabilities: Any,\n) -> None:\n    \"\"\"\n    >>> _validate_dicts({\"c\":0.5}, {\"d\": {\"e\": 0.6}}, {\"f\": {\"g\": 0.7}})\n    >>> _validate_dicts(\"invalid\", {\"d\": {\"e\": 0.6}}, {\"f\": {\"g\": 0.7}})\n    Traceback (most recent call last):\n            ...\n    ValueError: initial_probabilities must be a dict","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/viterbi.py#L260-L296","documentation":"Raised by viterbi's internal _validate_list (dynamic_programming/viterbi.py:278) when observations_space or states_space is not a Python list. The Viterbi implementation indexes and iterates these collections assuming list semantics, and the validator deliberately rejects other types (tuples, strings, sets, ints) with a message naming the offending parameter via var_name.","triggerScenarios":"Calling viterbi with observations_space=('rainy','sunny') (a tuple) or a numpy array instead of a list; passing a string like 'abcd' (a str is not a list); passing an int. The message interpolates the parameter name, e.g. 'observations_space must be a list'.","commonSituations":"Passing numpy array columns or pandas Series directly from a data pipeline without .tolist(); using tuples for 'immutability' in config; assuming duck typing accepts any iterable.","solutions":["Convert with list(...) at the call site: viterbi(list(observations_space), list(states_space), ...).","For numpy arrays use .tolist() to get native Python str elements.","Keep the HMM config format aligned with the API (JSON arrays deserialize to lists naturally)."],"exampleFix":"# before\nviterbi(('walk','shop'), ['rainy','sunny'], initial_p, trans_p, emit_p)  # ValueError\n\n# after\nviterbi(['walk','shop'], ['rainy','sunny'], initial_p, trans_p, emit_p)","handlingStrategy":"type-guard","validationCode":"def as_str_list(x) -> list[str] | None:\n    return list(x) if isinstance(x, (list, tuple, set)) and all(isinstance(i, str) for i in x) else None","typeGuard":"def is_str_list(x: object) -> TypeGuard[list[str]]:\n    return isinstance(x, list) and all(isinstance(i, str) for i in x)","tryCatchPattern":null,"preventionTips":["Convert numpy arrays with .tolist() at the pipeline boundary.","Standardize on plain lists for HMM symbol spaces.","Keep config in JSON so arrays deserialize as lists."],"tags":["dynamic-programming","hmm","type-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}