polars.Expr.list.contains#

Expr.list.contains(item: IntoExpr, *, nulls_equal: bool = True) Expr[source]#

Check if sublists contain the given item.

Parameters:
item

Item that will be checked for membership

nulls_equalbool, default True

If True, treat null as a distinct value. Null values will not propagate.

Returns:
Expr

Expression of data type Boolean.

Examples

>>> df = pl.DataFrame({"a": [[3, 2, 1], [], [1, 2]]})
>>> df.with_columns(contains=pl.col("a").list.contains(1))
shape: (3, 2)
┌───────────┬──────────┐
│ a         ┆ contains │
│ ---       ┆ ---      │
│ list[i64] ┆ bool     │
╞═══════════╪══════════╡
│ [3, 2, 1] ┆ true     │
│ []        ┆ false    │
│ [1, 2]    ┆ true     │
└───────────┴──────────┘