{"record":{"id":"76b9c2e767a3fd43","repo":"pola-rs/polars","slug":"list-to-struct-got-a-str-instead-of-a-list-hint","errorCode":null,"errorMessage":"list.to_struct() got a str instead of a list. hint: pass ['{fields}'] instead of '{fields}'","messagePattern":"list\\.to_struct\\(\\) got a str instead of a list\\. hint: pass \\['(.+?)'\\] instead of '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/list.py","lineNumber":1384,"sourceCode":"        ┌──────┬──────┐\n        │ x    ┆ y    │\n        │ ---  ┆ ---  │\n        │ i64  ┆ i64  │\n        ╞══════╪══════╡\n        │ 1    ┆ null │\n        │ 0    ┆ 1    │\n        │ 1    ┆ 0    │\n        │ null ┆ null │\n        │ null ┆ 1    │\n        │ null ┆ null │\n        └──────┴──────┘\n        \"\"\"\n        if isinstance(fields, str):\n            msg = (\n                \"list.to_struct() got a str instead of a list. \"\n                f\"hint: pass ['{fields}'] instead of '{fields}'\"\n            )\n            raise TypeError(msg)\n\n        return wrap_expr(self._pyexpr.list_to_struct(fields))\n\n    def to_map(self) -> Expr:\n        \"\"\"\n        Convert the `List` of `Struct` entries to a `Map`.\n\n        The input must be a `List` of `Struct` with exactly two fields named `key`\n        and `value`. Keys must not be null; duplicate keys within a row are resolved\n        by keeping the first position and the last value.\n\n        The inverse of :meth:`Expr.map.entries`.\n\n        .. engine-support:: in-memory, streaming, distributed\n\n        Examples\n        --------\n        >>> df = pl.DataFrame(","sourceCodeStart":1366,"sourceCodeEnd":1402,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/expr/list.py#L1366-L1402","documentation":"Expr.list.to_struct() expects `fields` to be a list of field-name strings. Passing a bare string means you likely wrote the single field name directly; Polars raises this TypeError with a hint telling you to wrap the string in a list. This guards against ambiguity between one field name and several.","triggerScenarios":"Calling Expr.list.to_struct(fields='a') instead of fields=['a'].","commonSituations":"Single-field structs built from one-element lists; converting code that previously accepted a string; typo when defining field order for the resulting struct dtype.","solutions":["Wrap the string in a list: to_struct(['a']).","If multiple fields, pass them in the desired order: to_struct(['a', 'b']).","If you wanted default field names, omit `fields` entirely: to_struct()."],"exampleFix":"// before\npl.col('items').list.to_struct('a')\n// after\npl.col('items').list.to_struct(['a'])","handlingStrategy":"type-guard","validationCode":"if isinstance(fields, str):\n    fields = [fields]","typeGuard":"def as_field_list(fields):\n    return [fields] if isinstance(fields, str) else fields","tryCatchPattern":"try:\n    expr = pl.col('a').list.to_struct(fields)\nexcept TypeError:\n    expr = pl.col('a').list.to_struct([fields])","preventionTips":["Always pass field names as a list, even for a single field.","Normalize str inputs to [str] in shared helper functions.","Read the to_struct signature; fields is Sequence[str]."],"tags":["python","polars","type-error","arguments"],"backgroundTag":"invalid-argument-value","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"}