Logo

Programming-Idioms

Remove all the elements from list x that don't satisfy the predicate p, without allocating a new list.
Keep all the elements that do satisfy p.

For languages that don't have mutable lists, refer to idiom #57 instead.
Implementation
Perl

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Perl implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
j := 0
for i, v := range x {
	if p(v) {
		x[j] = x[i]
		j++
	}
}
x = x[:j]
j := 0
for i, v := range x {
	if p(v) {
		x[j] = x[i]
		j++
	}
}
for k := j; k < len(x); k++ {
	x[k] = nil
}
x = x[:j]
uses classes;
for i := x.count-1 downto 0 do
  if not p(x.items[i]) then x.delete(i);
#include <list>
#include <functional>
std::list<Foo> x;
x.remove_if(std::not_fn(p));
#include <functional>
std::erase_if(x, std::not_fn(p));
x.select!(&:p)
for (const [key, value] of x.entries()) {
	if (!p(value)) x.splice(key, 1);
}
let mut j = 0;
for i in 0..x.len() {
    if p(x[i]) {
        x[j] = x[i];
        j += 1;
    }
}
x.truncate(j);
x.retain(p);
del_count = 0
for i in range(len(x)):
    if not p(x[i - del_count]):
        del x[i - del_count]
        del_count += 1
x.RemoveAll(item => !p(item));
x = pack(x,.not.p())
x.retainWhere((e) => p);
x.removeIf(p.negate());
func Filter[S ~[]T, T any](x *S, p func(T) bool) {
	j := 0
	for i, v := range *x {
		if p(v) {
			(*x)[j] = (*x)[i]
			j++
		}
	}
	var zero T
	for k := j; k < len(*x); k++ {
		(*x)[k] = zero
	}
	*x = (*x)[:j]
}
x = x.filter((e) => p(e));
import "slices"
del := func(t *T) bool { return !p(t) }

x = slices.DeleteFunc(x, del)