Skip to content

daft.functions.list_map#

list_map #

list_map(list_expr: Expression, mapper: Expression) -> Expression

Evaluates an expression on all elements in the list.

Parameters:

Name Type Description Default
list_expr List Expression

expression to map over.

required
mapper Expression

Expression to run. You can select the element with daft.element()

required

Returns:

Name Type Description
Expression List Expression

an expression representing the mapped list.

Examples:

1
2
3
4
>>> import daft
>>> from daft.functions import list_map, upper
>>> df = daft.from_pydict({"letters": [["a", "b", "a"], ["b", "c", "b", "c"]]})
>>> df.with_column("letters_capitalized", list_map(df["letters"], upper(daft.element()))).collect()
╭──────────────┬─────────────────────╮
│ letters      ┆ letters_capitalized │
│ ---          ┆ ---                 │
│ List[String] ┆ List[String]        │
╞══════════════╪═════════════════════╡
│ [a, b, a]    ┆ [A, B, A]           │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [b, c, b, c] ┆ [B, C, B, C]        │
╰──────────────┴─────────────────────╯
(Showing first 2 of 2 rows)
Source code in daft/functions/list.py
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
def list_map(list_expr: Expression, mapper: Expression) -> Expression:
    """Evaluates an expression on all elements in the list.

    Args:
        list_expr (List Expression): expression to map over.
        mapper: Expression to run. You can select the element with `daft.element()`

    Returns:
        Expression (List Expression): an expression representing the mapped list.

    Examples:
        >>> import daft
        >>> from daft.functions import list_map, upper
        >>> df = daft.from_pydict({"letters": [["a", "b", "a"], ["b", "c", "b", "c"]]})
        >>> df.with_column("letters_capitalized", list_map(df["letters"], upper(daft.element()))).collect()
        ╭──────────────┬─────────────────────╮
        │ letters      ┆ letters_capitalized │
        │ ---          ┆ ---                 │
        │ List[String] ┆ List[String]        │
        ╞══════════════╪═════════════════════╡
        │ [a, b, a]    ┆ [A, B, A]           │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ [b, c, b, c] ┆ [B, C, B, C]        │
        ╰──────────────┴─────────────────────╯
        <BLANKLINE>
        (Showing first 2 of 2 rows)
    """
    return Expression._call_builtin_scalar_fn("list_map", list_expr, mapper)