Skip to content

daft.functions.to_list#

to_list #

to_list(*items: Expression) -> Expression

Constructs a list from the item expressions.

Parameters:

Name Type Description Default
*items Expression

item expressions to construct the list

()

Returns:

Name Type Description
Expression List Expression

expression representing the constructed list

Examples:

1
2
3
4
5
6
>>> import daft
>>> from daft.functions import to_list
>>>
>>> df = daft.from_pydict({"x": [1, 2, 3], "y": [4, 5, 6]})
>>> df = df.select(to_list(df["x"], df["y"]).alias("fwd"), to_list(df["y"], df["x"]).alias("rev"))
>>> df.show()
╭─────────────┬─────────────╮
│ fwd         ┆ rev         │
│ ---         ┆ ---         │
│ List[Int64] ┆ List[Int64] │
╞═════════════╪═════════════╡
│ [1, 4]      ┆ [4, 1]      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [2, 5]      ┆ [5, 2]      │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [3, 6]      ┆ [6, 3]      │
╰─────────────┴─────────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/list.py
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
def to_list(*items: Expression) -> Expression:
    """Constructs a list from the item expressions.

    Args:
        *items: item expressions to construct the list

    Returns:
        Expression (List Expression): expression representing the constructed list

    Examples:
        >>> import daft
        >>> from daft.functions import to_list
        >>>
        >>> df = daft.from_pydict({"x": [1, 2, 3], "y": [4, 5, 6]})
        >>> df = df.select(to_list(df["x"], df["y"]).alias("fwd"), to_list(df["y"], df["x"]).alias("rev"))
        >>> df.show()
        ╭─────────────┬─────────────╮
        │ fwd         ┆ rev         │
        │ ---         ┆ ---         │
        │ List[Int64] ┆ List[Int64] │
        ╞═════════════╪═════════════╡
        │ [1, 4]      ┆ [4, 1]      │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ [2, 5]      ┆ [5, 2]      │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ [3, 6]      ┆ [6, 3]      │
        ╰─────────────┴─────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    assert len(items) > 0, "List constructor requires at least one item"
    return Expression._from_pyexpr(list_([Expression._to_expression(i)._expr for i in items]))