Skip to content

Check if sub-lists contains a given value

Description

Check if sub-lists contains a given value

Usage

<Expr>$list$contains(item)

Arguments

item Item that will be checked for membership. Can be an Expr or something coercible to an Expr. Strings are not parsed as columns.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  a = list(3:1, NULL, 1:2),
  item = 0:2
)
df$with_columns(
  with_expr = pl$col("a")$list$contains(pl$col("item")),
  with_lit = pl$col("a")$list$contains(1)
)
#> shape: (3, 4)
#> ┌───────────┬──────┬───────────┬──────────┐
#> │ a         ┆ item ┆ with_expr ┆ with_lit │
#> │ ---       ┆ ---  ┆ ---       ┆ ---      │
#> │ list[i32] ┆ i32  ┆ bool      ┆ bool     │
#> ╞═══════════╪══════╪═══════════╪══════════╡
#> │ [3, 2, 1] ┆ 0    ┆ false     ┆ true     │
#> │ null      ┆ 1    ┆ null      ┆ null     │
#> │ [1, 2]    ┆ 2    ┆ true      ┆ true     │
#> └───────────┴──────┴───────────┴──────────┘