Skip to content

Get the intersection of two list variables

Source code

Description

Get the intersection of two list variables

Usage

<Expr>$list$set_intersection(other)

Arguments

other Other list variable. Can be an Expr or something coercible to an Expr.

Details

Note that the datatypes inside the list must have a common supertype. For example, the first column can be list[i32] and the second one can be list[i8] because it can be cast to list[i32]. However, the second column cannot be e.g list[f32].

Value

Expr

Examples

library("polars")

df = pl$DataFrame(
  a = list(1:3, NA_integer_, c(NA_integer_, 3L), 5:7),
  b = list(2:4, 3L, c(3L, 4L, NA_integer_), c(6L, 8L))
)

df$with_columns(intersection = pl$col("a")$list$set_intersection("b"))
#> shape: (4, 3)
#> ┌───────────┬──────────────┬──────────────┐
#> │ a         ┆ b            ┆ intersection │
#> │ ---       ┆ ---          ┆ ---          │
#> │ list[i32] ┆ list[i32]    ┆ list[i32]    │
#> ╞═══════════╪══════════════╪══════════════╡
#> │ [1, 2, 3] ┆ [2, 3, 4]    ┆ [2, 3]       │
#> │ [null]    ┆ [3]          ┆ []           │
#> │ [null, 3] ┆ [3, 4, null] ┆ [null, 3]    │
#> │ [5, 6, 7] ┆ [6, 8]       ┆ [6]          │
#> └───────────┴──────────────┴──────────────┘