79472614

Date: 2025-02-27 12:09:11
Score: 1
Natty:
Report link

You can also use list.set_symmetric_difference function. It computes the difference between two lists as a sets https://docs.pola.rs/api/python/stable/reference/expressions/api/polars.Expr.list.set_symmetric_difference.html#polars.Expr.list.set_symmetric_difference

The benefit is that the elements order inside compared lists can be arbitary

df = polars.DataFrame(dict(j=[
  [1,4,7],
  [2,5,8],
  [3,6,9],
  ]))

df.filter(
    pl.col("j").list.set_symmetric_difference(
        pl.lit([2, 5, 8])
    ).list.len() == 0
)

shape: (1, 1)
┌───────────┐
│ j         │
│ ---       │
│ list[i64] │
╞═══════════╡
│ [2, 5, 8] │
└───────────┘

or

df.filter(
    pl.col("j").list.set_symmetric_difference(
        pl.lit([8, 5, 2])
    ).list.len() == 0
)

shape: (1, 1)
┌───────────┐
│ j         │
│ ---       │
│ list[i64] │
╞═══════════╡
│ [2, 5, 8] │
└───────────┘
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: user2633719