Lesson Introduction

The filter function helps create filtered views of data. By the end of this lesson, you'll know how to use the filter function effectively, both with lambda functions and predefined functions.

Introducing the filter Function

The filter function is built into Python to create an iterator from elements of an iterable that satisfy a function. It’s useful for extracting specific elements from a list, tuple, or any iterable based on a condition.

Here’s the basic syntax:

  • function: A function that tests elements in the iterable.
  • iterable: Any iterable (e.g., list, tuple).
Example: Filtering Even Numbers

Let's see a basic example. Suppose you have a list of numbers and want to keep only the even numbers:

The is_even function checks if a number is even. The function keeps only elements for which returns .

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal