Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Python

Idiom #57 Filter list

Create the list y containing the items from the list x that satisfy the predicate p. Respect the original ordering. Don't modify x in-place.

y = [element for element in x if p(element)]

List comprehensions tend to be more readable than filter function
y = list(filter(p, x))

filter returns an iterator.
y = [*filter(p, x)]
for Item of X loop
   if P (Item) then
      Y.Append (Item);
   end if;
end loop;

New implementation...