{"record":{"id":"f2da204314bf393f","repo":"pola-rs/polars","slug":"the-length-of-value-name-len-values-does","errorCode":null,"errorMessage":"the length of `{value_name}` ({len(values)}) does not match the length of `{match_name}` ({n_match})","messagePattern":"the length of `(.+?)` \\((.+?)\\) does not match the length of `(.+?)` \\((.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/various.py","lineNumber":560,"sourceCode":"        del stack_frame\n\n    return objects\n\n\ndef extend_bool(\n    value: bool | Sequence[bool],  # noqa: FBT001\n    n_match: int,\n    value_name: str,\n    match_name: str,\n) -> Sequence[bool]:\n    \"\"\"Ensure the given bool or sequence of bools is the correct length.\"\"\"\n    values = [value] * n_match if isinstance(value, bool) else value\n    if n_match != len(values):\n        msg = (\n            f\"the length of `{value_name}` ({len(values)}) \"\n            f\"does not match the length of `{match_name}` ({n_match})\"\n        )\n        raise ValueError(msg)\n    return values\n\n\ndef in_terminal_that_supports_colour() -> bool:\n    \"\"\"\n    Determine (within reason) if we are in an interactive terminal that supports color.\n\n    Note: this is not exhaustive, but it covers a lot (most?) of the common cases.\n    \"\"\"\n    if hasattr(sys.stdout, \"isatty\"):\n        # can enhance as necessary, but this is a reasonable start\n        return (\n            sys.stdout.isatty()\n            and (\n                sys.platform != \"win32\"\n                or \"ANSICON\" in os.environ\n                or \"WT_SESSION\" in os.environ\n                or os.environ.get(\"TERM_PROGRAM\") == \"vscode\"","sourceCodeStart":542,"sourceCodeEnd":578,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/various.py#L542-L578","documentation":"extend_bool expands a scalar bool to match a length and then validates that the resulting list length equals the number of rows/keys of the match input (by/over, etc.). If the provided list's length differs from n_match, it raises ValueError stating both lengths and the parameter names. It guards sort_by/arg_sort_by/top_k-style APIs against misaligned per-row option lists.","triggerScenarios":"Calling sort/arg_sort_by/sort_by/top_k/top_k_by/bottom_k_by with by/over/descending/... option lists whose length doesn't equal the number of columns or rows expected, e.g. df.sort('a','b', descending=[True]) or df.top_k(k, by=['x','y'], reverse=[True]).","commonSituations":"Adding/removing a column in the `by`/`over` list without updating the parallel options list; passing multiple columns but a single-element list where a scalar would have been accepted.","solutions":["Make the list length match: descending=[True, False] for two `by` columns, or pass a scalar bool instead of a list.","Compute lengths programmatically: descending=[True]*len(by_cols).","Double-check that value_name and match_name in the message refer to the parameters you intended to align."],"exampleFix":"// before\ndf.sort(['a', 'b'], descending=[True])\n// after\ndf.sort(['a', 'b'], descending=[True, False])","handlingStrategy":"validation","validationCode":"assert len(descending) == len(by_cols), f'descending ({len(descending)}) != by ({len(by_cols)})'","typeGuard":"def lengths_match(a, b) -> bool:\n    return len(a) == len(b)","tryCatchPattern":"try:\n    df.sort(by_cols, descending=descending)\nexcept ValueError as e:\n    if 'does not match the length of' in str(e):\n        descending = descending * len(by_cols) if len(descending) == 1 else [False]*len(by_cols)\n        df = df.sort(by_cols, descending=descending)","preventionTips":["Pass scalar bools when one setting applies to all columns","Derive option lists with [x]*len(by_cols)","Re-check parallel lists after editing `by`/`over` columns"],"tags":["python","polars","length-mismatch","arguments"],"backgroundTag":"shape-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}