Skip to main content

Filtering a Collection

Filtering means picking out the elements of a collection that match a condition.

The condition takes one element and answers True (keep it) or False (skip it). A function that does this is called a predicate.

Filtering builds a new list. The original list is not changed.

In Python you filter with a list comprehension that has an if part: [w for w in words if len(w) > 3]. It reads almost like English: keep every w whose length is greater than 3.

Python also has a built-in filter() function that takes a predicate and a list. It works, but it returns a lazy iterator and needs a lambda for a simple condition, so the comprehension is usually clearer. This page uses comprehensions.


Filter by Predicate

The simplest way to filter is a comprehension whose if part holds the condition. Python walks through the list and keeps every element that passes the test.

Dictionaries work the same way: a dict comprehension over items() copies the matching key:value pairs into a new dictionary.

If the condition also needs the position of the element, enumerate gives you the index and the value together.

To keep the elements that do not match, put not in front of the condition. Python has no separate filter_not.

A list can hold values of different types. isinstance(v, str) checks whether a value is a string. A value can also be None, which means "no value"; compare with is not None to skip such entries.

Filter a list

Keep only the words that are longer than 3 letters. The condition len(w) > 3 sits in the if part of the comprehension.

Prompt
Write a Python program with the words one, two, three, four in a list. Use a list comprehension with an if part to keep the words longer than 3 letters. Print the result.
Output: complete Python program
Filter a list

Filter a dictionary

Loop over items() and keep the pairs whose key ends with "1" and whose value is greater than 10. endswith() checks the end of a string.

Prompt
Create a dictionary from the keys key1, key2, key3, key11 to the values 1, 2, 3, 11. Use a dict comprehension over items() to keep only the pairs whose key ends with "1" and whose value is greater than 10. Print the new dictionary.
Output: complete Python program
Filter a dictionary

Filter using the index

enumerate gives the index and the value. Keep a word only when its index is not 0 and it has fewer than 5 letters.

Prompt
Take the words one, two, three, four. Use enumerate in a list comprehension so the condition sees both the index and the word. Keep a word only if its index is not 0 and it is shorter than 5 letters. Print the kept words.
Output: complete Python program
Filter using the index

Filter with a negated condition

Drop the words with 3 letters or fewer. The condition len(w) = 3 is negated with not, so only the longer words stay.

Prompt
Take the words one, two, three, four. Filter with a list comprehension, but negate the condition with not so that words with 3 letters or fewer are dropped. Print the result.
Output: complete Python program
Filter with a negated condition

Filter by type

The list holds None, a number, two strings and a float. isinstance(v, str) finds the strings. Each one is printed in upper case.

Prompt
Make a list holding None, 1, "two", 3.0 and "four". Loop over it and use isinstance to find the values that are strings. Print each string in upper case, one per line.
Output: complete Python program
Filter by type

Filter out None values

The list holds strings, but some entries are None. Keep the values that are not None with the is not None check.

Prompt
Make a list that holds None, "one", None, and "two". Keep only the values that are not None with a list comprehension. Print the collected list.
Output: complete Python program
Filter out None values

Partition

Sometimes you want both groups: the elements that match and the ones that do not.

Python has no built-in partition for lists. A small helper does it: it loops once and appends each element to either the match list or the rest list. The helper returns both lists, because a Python function can return more than one value.

You could also write two comprehensions, one with the condition and one with its negation, but that walks the list twice; the helper does one pass.

Split a list in two

Words longer than 3 letters go into match; all the others go into rest. Both lists are printed.

Prompt
Write a partition helper function that splits a list into two: the elements that pass a condition and the rest. Use it on the words one, two, three, four with the condition "longer than 3 letters". Print the matching list, then the rest.
Output: complete Python program
Split a list in two

Test Predicates

Sometimes you do not need the matching elements, only a yes or no answer. Python has built-in functions for this:

  • any: does at least one element pass the test? any() answers this.
  • none: does no element pass the test? Put not in front of any().
  • all: does every element pass the test? all() answers this.

Both take a series of True/False values, so you pass them a generator expression such as w.endswith("e") for w in numbers. any() stops as soon as it finds a match.

Without a condition, "any element at all?" is len(numbers) > 0 and "no element at all?" is len(numbers) == 0.

On an empty list, all() returns True. There is no element that fails the test, so the test is never broken. This is called vacuous truth.

any

Check whether at least one word ends with "e", then check whether the list has any element at all.

Prompt
Take the words one, two, three, four. Use the built-in any() with a generator expression to check whether at least one word ends with "e" and print the answer. Then print whether the list has any element at all by checking its length is greater than 0.
Output: complete Python program
any

none

Check that no word ends with "a", then check whether the list is empty.

Prompt
Take the words one, two, three, four. Use not with any() to check that no word ends with "a" and print the answer. Then print whether the list is empty by checking its length equals 0.
Output: complete Python program
none

all

Check whether every word ends with "e". Then call all() on an empty list, which gives True.

Prompt
Take the words one, two, three, four. Use the built-in all() to check whether every word ends with "e" and print the answer. Then call all() on an empty list with the same condition and print the answer.
Output: complete Python program
all