Skip to content

daft.functions.list_contains#

list_contains #

list_contains(list_expr: Expression, item: Expression) -> Expression

Checks if each list contains the specified item.

Parameters:

Name Type Description Default
list_expr Expression

expression to search in

required
item Expression

value or column of values to search for

required

Returns:

Type Description
Expression

Boolean expression indicating whether each list contains the item

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import list_contains
>>>
>>> df = daft.from_pydict({"a": [[1, 2, 3], [2, 4], [1, 3, 5], []]})
>>> df.where(list_contains(df["a"], 3)).show()
╭─────────────╮
│ a           │
│ ---         │
│ List[Int64] │
╞═════════════╡
│ [1, 2, 3]   │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ [1, 3, 5]   │
╰─────────────╯
(Showing first 2 of 2 rows)
1
2
3
>>> # Check against another column
>>> df2 = daft.from_pydict({"lists": [[1, 2], [3, 4]], "items": [1, 4]})
>>> df2.with_column("match", list_contains(df2["lists"], df2["items"])).show()
╭─────────────┬───────┬───────╮
│ lists       ┆ items ┆ match │
│ ---         ┆ ---   ┆ ---   │
│ List[Int64] ┆ Int64 ┆ Bool  │
╞═════════════╪═══════╪═══════╡
│ [1, 2]      ┆ 1     ┆ true  │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
│ [3, 4]      ┆ 4     ┆ true  │
╰─────────────┴───────┴───────╯
(Showing first 2 of 2 rows)
Source code in daft/functions/list.py
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
def list_contains(list_expr: Expression, item: Expression) -> Expression:
    """Checks if each list contains the specified item.

    Args:
        list_expr: expression to search in
        item: value or column of values to search for

    Returns:
        Boolean expression indicating whether each list contains the item

    Examples:
        >>> import daft
        >>> from daft.functions import list_contains
        >>>
        >>> df = daft.from_pydict({"a": [[1, 2, 3], [2, 4], [1, 3, 5], []]})
        >>> df.where(list_contains(df["a"], 3)).show()
        ╭─────────────╮
        │ a           │
        │ ---         │
        │ List[Int64] │
        ╞═════════════╡
        │ [1, 2, 3]   │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ [1, 3, 5]   │
        ╰─────────────╯
        <BLANKLINE>
        (Showing first 2 of 2 rows)

        >>> # Check against another column
        >>> df2 = daft.from_pydict({"lists": [[1, 2], [3, 4]], "items": [1, 4]})
        >>> df2.with_column("match", list_contains(df2["lists"], df2["items"])).show()
        ╭─────────────┬───────┬───────╮
        │ lists       ┆ items ┆ match │
        │ ---         ┆ ---   ┆ ---   │
        │ List[Int64] ┆ Int64 ┆ Bool  │
        ╞═════════════╪═══════╪═══════╡
        │ [1, 2]      ┆ 1     ┆ true  │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┤
        │ [3, 4]      ┆ 4     ┆ true  │
        ╰─────────────┴───────┴───────╯
        <BLANKLINE>
        (Showing first 2 of 2 rows)
    """
    return Expression._call_builtin_scalar_fn("list_contains", list_expr, item)