df = pl.DataFrame({'list_column': [['a.xml', 'b.xml', 'c', 'd'], ['e.xml', 'f.xml', 'g', 'h']]})
def func(x):
return [y for y in x if '.xml' in y]
df.with_columns(pl.col('list_column').map_elements(func, return_dtype=pl.List(pl.String)))
Is there a way to achieve the same without using map_elements?
>Solution :
There is an accepted request for list.filter()
You can emulate the behaviour using list.eval()
df.with_columns(
pl.col('list_column').list.eval(
pl.element().filter(pl.element().str.ends_with('.xml'))
)
)
shape: (2, 1)
┌────────────────────┐
│ list_column │
│ --- │
│ list[str] │
╞════════════════════╡
│ ["a.xml", "b.xml"] │
│ ["e.xml", "f.xml"] │
└────────────────────┘