Cara menggunakan python filter reverse

The filter[] function extracts elements from an iterable [list, tuple etc.] for which a function returns True.

Example

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# returns True if number is even
def check_even[number]:
    if number % 2 == 0:
          return True  

    return False

# Extract elements from the numbers list for which check_even[] returns True even_numbers_iterator = filter[check_even, numbers]

# converting to list even_numbers = list[even_numbers_iterator] print[even_numbers] # Output: [2, 4, 6, 8, 10]

filter[] Syntax

Its syntax is:

filter[function, iterable]

filter[] Arguments

The filter[] function takes two arguments:

filter[] Return Value

The filter[] function returns an iterator.

Note: You can easily convert iterators to sequences like lists, tuples, strings etc.

Example 1: Working of filter[]

letters = ['a', 'b', 'd', 'e', 'i', 'j', 'o']

# a function that returns True if letter is vowel
def filter_vowels[letter]:
    vowels = ['a', 'e', 'i', 'o', 'u']
    return True if letter in vowels else False

filtered_vowels = filter[filter_vowels, letters]

# converting to tuple vowels = tuple[filtered_vowels] print[vowels]

Output

['a', 'e', 'i', 'o']

Here, the filter[] function extracts only the vowel letters from the

filter[function, iterable]
3 list. Here's how this code works:

  • Each element of the
    filter[function, iterable]
    3 list is passed to the
    filter[function, iterable]
    5 function.
  • If
    filter[function, iterable]
    5 returns True, that element is extracted otherwise it's filtered out.

Note: It's also possible to filter lists using a loop, however, using the filter[] function is much more cleaner.

Example 2: Using Lambda Function Inside filter[]

numbers = [1, 2, 3, 4, 5, 6, 7]

# the lambda function returns True for even numbers 
even_numbers_iterator = filter[lambda x: [x%2 == 0], numbers]

# converting to list
even_numbers = list[even_numbers_iterator]

print[even_numbers]

Output

[2, 4, 6]

Here, we have directly passed a lambda function inside filter[].

Our lambda function returns True for even numbers. Hence, the filter[] function returns an iterator containing even numbers only.

Example 3: Using None as a Function Inside filter[]

# random list
random_list = [1, 'a', 0, False, True, '0']

filtered_iterator = filter[None, random_list]

#converting to list filtered_list = list[filtered_iterator] print[filtered_list]

Output

[1, 'a', True, '0']

When

letters = ['a', 'b', 'd', 'e', 'i', 'j', 'o']

# a function that returns True if letter is vowel
def filter_vowels[letter]:
    vowels = ['a', 'e', 'i', 'o', 'u']
    return True if letter in vowels else False

filtered_vowels = filter[filter_vowels, letters]

# converting to tuple vowels = tuple[filtered_vowels] print[vowels]
2 is used as the first argument to the filter[] function, all elements that are truthy values [gives True if converted to boolean] are extracted.

Python’s is a built-in function that allows you to process an iterable and extract those items that satisfy a given condition. This process is commonly known as a filtering operation. With

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4, you can apply a filtering function to an iterable and produce a new iterable with the items that satisfy the condition at hand. In Python,
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 is one of the tools you can use for functional programming.

In this tutorial, you’ll learn how to:

  • Use Python’s
    >>> numbers = [-2, -1, 0, 1, 2]
    
    >>> # Using a lambda function
    >>> positive_numbers = filter[lambda n: n > 0, numbers]
    >>> positive_numbers
    
    >>> list[positive_numbers]
    [1, 2]
    
    >>> # Using a user-defined function
    >>> def is_positive[n]:
    ...     return n > 0
    ...
    >>> list[filter[is_positive, numbers]]
    [1, 2]
    
    4 in your code
  • Extract needed values from your iterables
  • Combine
    >>> numbers = [-2, -1, 0, 1, 2]
    
    >>> # Using a lambda function
    >>> positive_numbers = filter[lambda n: n > 0, numbers]
    >>> positive_numbers
    
    >>> list[positive_numbers]
    [1, 2]
    
    >>> # Using a user-defined function
    >>> def is_positive[n]:
    ...     return n > 0
    ...
    >>> list[filter[is_positive, numbers]]
    [1, 2]
    
    4 with other functional tools
  • Replace
    >>> numbers = [-2, -1, 0, 1, 2]
    
    >>> # Using a lambda function
    >>> positive_numbers = filter[lambda n: n > 0, numbers]
    >>> positive_numbers
    
    >>> list[positive_numbers]
    [1, 2]
    
    >>> # Using a user-defined function
    >>> def is_positive[n]:
    ...     return n > 0
    ...
    >>> list[filter[is_positive, numbers]]
    [1, 2]
    
    4 with more Pythonic tools

With this knowledge, you’ll be able to use

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 effectively in your code. Alternatively, you have the choice of using list comprehensions or to write more Pythonic and readable code.

To better understand

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4, it would be helpful for you to have some previous knowledge on ,
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
2 loops, functions, and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
3 functions.

Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

Coding With Functional Style in Python

Functional programming is a paradigm that promotes using functions to perform almost every task in a program. A pure functional style relies on functions that don’t modify their input arguments and don’t change the program’s state. They just take a specific set of arguments and return the same result every time. These kinds of functions are known as pure functions.

In functional programming, functions often operate on arrays of data, transform them, and produce new arrays with added features. There are three fundamental operations in functional programming:

  1. Mapping applies a transformation function to an iterable and produces a new iterable of transformed items.
  2. Filtering applies a predicate, or Boolean-valued, function to an iterable and generates a new iterable containing the items that satisfy the Boolean condition.
  3. Reducing applies a reduction function to an iterable and returns a single cumulative value.

Python isn’t heavily influenced by functional languages but by imperative ones. However, it provides several features that allow you to use a functional style:

  • Anonymous functions
  • A
    >>> def identity[x]:
    ...     return x
    ...
    
    >>> identity[42]
    42
    
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    >>> list[filter[identity, objects]]
    [1, 4, 5, 8]
    
    4 function
  • A function
  • A
    >>> def identity[x]:
    ...     return x
    ...
    
    >>> identity[42]
    42
    
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    >>> list[filter[identity, objects]]
    [1, 4, 5, 8]
    
    6 function

Functions in Python are , which means that you can pass them around as you’d do with any other object. You can also use them as arguments and return values of other functions. Functions that accept other functions as arguments or that return functions [or both] are known as higher-order functions, which are also a desirable feature in functional programming.

In this tutorial, you’ll learn about

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4. This built-in function is one of the more popular functional tools of Python.

Remove ads

Understanding the Filtering Problem

Say you need to process a list of numbers and return a new list containing only those numbers greater than

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
8. A quick way to approach this problem is to use a
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
2 loop like this:

>>>

>>> numbers = [-2, -1, 0, 1, 2]

>>> def extract_positive[numbers]:
...     positive_numbers = []
...     for number in numbers:
...         if number > 0:  # Filtering condition
...             positive_numbers.append[number]
...     return positive_numbers
...

>>> extract_positive[numbers]
[1, 2]

The loop in

>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
0 iterates through
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
1 and stores every number greater than
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
8 in
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
3. The conditional statement filters out the negative numbers and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
8. This kind of functionality is known as a filtering.

Filtering operations consist of testing each value in an iterable with a predicate function and retaining only those values for which the function produces a true result. Filtering operations are fairly common in programming, so most programming languages provide tools to approach them. In the next section, you’ll learn about Python’s way to filter iterables.

Getting Started With Python’s
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4

Python provides a convenient built-in function,

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4, that abstracts out the logic behind filtering operations. Here’s its signature:

filter[function, iterable]

The first argument,

>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
7, must be a single-argument function. Typically, you provide a predicate [Boolean-valued] function to this argument. In other words, you provide a function that returns either
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8 or
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
9 according to a specific condition.

This

>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
7 plays the role of a decision function, also known as a filtering function, because it provides the criteria to filter out unwanted values from the input iterable and to keep those values that you want in the resulting iterable. Note that the term unwanted values refers to those values that evaluate to false when
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 processes them using
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
7.

Note: The first argument to

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 is a function object, which means that you need to pass a function without calling it with a pair of parentheses.

The second argument,

>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]
4, can hold any Python iterable, such as a list, tuple, or set. It can also hold generator and iterator objects. An important point regarding
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 is that it accepts only one
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]
4.

To perform the filtering process,

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 applies
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
7 to every item of
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]
4 in a loop. The result is an iterator that yields the values of
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]
4 for which
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
7 returns a true value. The process doesn’t modify the original input iterable.

Since

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 is written in C and is highly optimized, its internal implicit loop can be more efficient than a regular
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
2 loop regarding execution time. This efficiency is arguably the most important advantage of using the function in Python.

A second advantage of using

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 over a loop is that it returns a
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def is_even[number]:
...     return number % 2 == 0  # Filtering condition
...

>>> list[filter[is_even, numbers]]
[10, 6, 50]
5 object, which is an iterator that yields values on demand, promoting a lazy evaluation strategy. Returning an iterator makes
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 more memory efficient than an equivalent
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
2 loop.

Note: In Python 2.x, returns

>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def is_even[number]:
...     return number % 2 == 0  # Filtering condition
...

>>> list[filter[is_even, numbers]]
[10, 6, 50]
9 objects. This behavior changed in . Now the function returns a
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def is_even[number]:
...     return number % 2 == 0  # Filtering condition
...

>>> list[filter[is_even, numbers]]
[10, 6, 50]
5 object, which is an iterator that yields items on demand. Python iterators are well known to be memory efficient.

In your example about positive numbers, you can use

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 along with a convenient predicate function to extract the desired numbers. To code the predicate, you can use either a
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
3 or a user-defined function:

>>>

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]

In the first example, you use a

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
3 function that provides the filtering functionality. The call to
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 applies that
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
3 function to every value in
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
1 and filters out the negative numbers and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
8. Since
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 returns an iterator, you need to call
>>> import math

>>> def is_prime[n]:
...     if n >> is_prime[5]
True
>>> is_prime[12]
False
9 to consume the iterator and create the final list.

Note: Since

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 is a built-in function, you don’t have to import anything to be able to use it in your code.

In the second example, you write

>>> list[filter[is_prime, range[1, 51]]]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
1 to take a number as an argument and return
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8 if the number is greater than
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
8. Otherwise, it returns
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
9. The call to
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 applies
>>> list[filter[is_prime, range[1, 51]]]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
1 to every value in
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
1, filtering out the negative numbers. This solution is way more readable than its
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
3 equivalent.

In practice,

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 isn’t limited to Boolean functions such as those in the examples above. You can use other types of functions, and
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 will evaluate their return value for truthiness:

>>>

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]

In this example, the filtering function,

>>> import statistics as st
>>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]

>>> # The mean before removing outliers
>>> mean = st.mean[sample]
>>> mean
10.692307692307692

>>> stdev = st.stdev[sample]
>>> low = mean - 2 * stdev
>>> high = mean + 2 * stdev

>>> clean_sample = list[filter[lambda x: low > clean_sample
[10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]

>>> # The mean after removing outliers
>>> st.mean[clean_sample]
8.75
1, doesn’t return
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8 or
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
9 explicitly but the same argument it takes. Since
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
8,
>>> import statistics as st
>>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]

>>> # The mean before removing outliers
>>> mean = st.mean[sample]
>>> mean
10.692307692307692

>>> stdev = st.stdev[sample]
>>> low = mean - 2 * stdev
>>> high = mean + 2 * stdev

>>> clean_sample = list[filter[lambda x: low > clean_sample
[10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]

>>> # The mean after removing outliers
>>> st.mean[clean_sample]
8.75
5,
>>> import statistics as st
>>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]

>>> # The mean before removing outliers
>>> mean = st.mean[sample]
>>> mean
10.692307692307692

>>> stdev = st.stdev[sample]
>>> low = mean - 2 * stdev
>>> high = mean + 2 * stdev

>>> clean_sample = list[filter[lambda x: low > clean_sample
[10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]

>>> # The mean after removing outliers
>>> st.mean[clean_sample]
8.75
6, and
>>> import statistics as st
>>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]

>>> # The mean before removing outliers
>>> mean = st.mean[sample]
>>> mean
10.692307692307692

>>> stdev = st.stdev[sample]
>>> low = mean - 2 * stdev
>>> high = mean + 2 * stdev

>>> clean_sample = list[filter[lambda x: low > clean_sample
[10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]

>>> # The mean after removing outliers
>>> st.mean[clean_sample]
8.75
7 are falsy,
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 uses their truth value to filter them out. The final list contains only those values that are truthy in Python.

Note: Python follows a set of rules to determine an object’s truth value.

For example, the following :

  • Constants like
    >>> import statistics as st
    >>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]
    
    >>> # The mean before removing outliers
    >>> mean = st.mean[sample]
    >>> mean
    10.692307692307692
    
    >>> stdev = st.stdev[sample]
    >>> low = mean - 2 * stdev
    >>> high = mean + 2 * stdev
    
    >>> clean_sample = list[filter[lambda x: low > clean_sample
    [10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]
    
    >>> # The mean after removing outliers
    >>> st.mean[clean_sample]
    8.75
    
    7 and
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    
    >>> list[filter[None, objects]]
    [1, 4, 5, 8]
    
    9
  • Numeric types with a zero value, like
    >>> def identity[x]:
    ...     return x
    ...
    
    >>> identity[42]
    42
    
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    >>> list[filter[identity, objects]]
    [1, 4, 5, 8]
    
    8,
    filter[function, iterable]
    
    02,
    filter[function, iterable]
    
    03, , and
  • Empty sequences and collections, like
    >>> import statistics as st
    >>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]
    
    >>> # The mean before removing outliers
    >>> mean = st.mean[sample]
    >>> mean
    10.692307692307692
    
    >>> stdev = st.stdev[sample]
    >>> low = mean - 2 * stdev
    >>> high = mean + 2 * stdev
    
    >>> clean_sample = list[filter[lambda x: low > clean_sample
    [10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]
    
    >>> # The mean after removing outliers
    >>> st.mean[clean_sample]
    8.75
    
    6,
    filter[function, iterable]
    
    07,
    >>> import statistics as st
    >>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]
    
    >>> # The mean before removing outliers
    >>> mean = st.mean[sample]
    >>> mean
    10.692307692307692
    
    >>> stdev = st.stdev[sample]
    >>> low = mean - 2 * stdev
    >>> high = mean + 2 * stdev
    
    >>> clean_sample = list[filter[lambda x: low > clean_sample
    [10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]
    
    >>> # The mean after removing outliers
    >>> st.mean[clean_sample]
    8.75
    
    5,
    filter[function, iterable]
    
    09,
    filter[function, iterable]
    
    10, and
    filter[function, iterable]
    
    11
  • Objects that implement with a return value of
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    
    >>> list[filter[None, objects]]
    [1, 4, 5, 8]
    
    9 or with a return value of
    >>> def identity[x]:
    ...     return x
    ...
    
    >>> identity[42]
    42
    
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    >>> list[filter[identity, objects]]
    [1, 4, 5, 8]
    
    8

Any other object will be considered truthy.

Finally, if you pass

>>> import statistics as st
>>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]

>>> # The mean before removing outliers
>>> mean = st.mean[sample]
>>> mean
10.692307692307692

>>> stdev = st.stdev[sample]
>>> low = mean - 2 * stdev
>>> high = mean + 2 * stdev

>>> clean_sample = list[filter[lambda x: low > clean_sample
[10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]

>>> # The mean after removing outliers
>>> st.mean[clean_sample]
8.75
7 to
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
7, then
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 uses the identity function and yields all the elements of
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]
4 that evaluate to
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8:

>>>

>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]

In this case,

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 tests every item in the input iterable using the Python rules you saw before. Then it yields those items that evaluate to
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8.

So far, you’ve learned the basics of

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 and how it works. In the following sections, you’ll learn how to use
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 to process iterables and throw away unwanted values without a loop.

Remove ads

Filtering Iterables With
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4

The job of

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 is to apply a decision function to each value in an input iterable and return a new iterable with those items that pass the test. The following sections provide some practical examples so you can get up and running with
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4.

Extracting Even Numbers

As a first example, say you need to process a list of integer numbers and build a new list containing the even numbers. Your first approach to this problem might be to use a

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
2 loop like this:

>>>

>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]

Here,

filter[function, iterable]
29 takes an iterable of integer numbers and returns a list containing only those that are even. The conditional statement plays the role of a filter that tests every number to find out if it’s even or not.

When you run into code like this, you can extract the filtering logic into a small predicate function and use it with

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4. This way, you can perform the same computation without using an explicit loop:

>>>

>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def is_even[number]:
...     return number % 2 == 0  # Filtering condition
...

>>> list[filter[is_even, numbers]]
[10, 6, 50]

Here,

filter[function, iterable]
31 takes an integer and returns
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8 if it’s even and
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
9 otherwise. The call to
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 does the hard work and filters out the odd numbers. As a result, you get a list of the even numbers. This code is shorter and more efficient than its equivalent
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
2 loop.

Finding Prime Numbers

Another interesting example might be to extract all the prime numbers in a given interval. To do that, you can start by coding a predicate function that takes an integer as an argument and returns

>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8 if the number is prime and
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
9 otherwise. Here’s how you can do that:

>>>

>>> import math

>>> def is_prime[n]:
...     if n >> is_prime[5]
True
>>> is_prime[12]
False

The filtering logic is now in

filter[function, iterable]
38. The function iterates through the integers between
filter[function, iterable]
39 and the square root of
filter[function, iterable]
40. Inside the loop, the conditional statement checks if the current number is divisible by any other in the interval. If so, then the function returns
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
9 because the number isn’t prime. Otherwise, it returns
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8 to signal that the input number is prime.

With

filter[function, iterable]
38 in place and tested, you can use
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 to extract prime numbers from an interval like this:

>>>

>>> list[filter[is_prime, range[1, 51]]]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

This call to

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 extracts all the prime numbers in the range between
filter[function, iterable]
46 and
filter[function, iterable]
47. The algorithm used in
filter[function, iterable]
38 comes from Wikipedia’s article about . You can check out that article if you need more efficient approaches.

Removing Outliers in a Sample

When you’re trying to describe and summarize a sample of data, you probably start by finding its mean, or average. The mean is a quite popular central tendency measurement and is often the first approach to analyzing a dataset. It gives you a quick idea of the center, or location, of the data.

In some cases, the mean isn’t a good enough central tendency measure for a given sample. are one of the elements that affect how accurate the mean is. Outliers are data points that differ significantly from other observations in a sample or population. Other than that, there is no unique mathematical definition for them in statistics.

However, in normally distributed samples, outliers are often defined as data points that lie more than two standard deviations from the sample mean.

Now suppose you have a normally distributed sample with some outliers that are affecting the mean accuracy. You’ve studied the outliers, and you know they’re incorrect data points. Here’s how you can use a couple of functions from the module along with

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 to clean up your data:

>>>

>>> import statistics as st
>>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]

>>> # The mean before removing outliers
>>> mean = st.mean[sample]
>>> mean
10.692307692307692

>>> stdev = st.stdev[sample]
>>> low = mean - 2 * stdev
>>> high = mean + 2 * stdev

>>> clean_sample = list[filter[lambda x: low > clean_sample
[10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]

>>> # The mean after removing outliers
>>> st.mean[clean_sample]
8.75

In the highlighted line, the

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
3 function returns
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8 if a given data point lies between the mean and two standard deviations. Otherwise, it returns
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
9. When you filter the
filter[function, iterable]
54 with this function,
filter[function, iterable]
55 is excluded. After this cleanup, the mean of the sample has a significantly different value.

Remove ads

Validating Python Identifiers

You can also use

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 with iterables containing nonnumeric data. For example, say you need to process a list of strings and extract those that are valid Python . After doing some research, you find out that Python’s provides a method called that can help you out with that validation.

Here’s how you can use

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 along with
filter[function, iterable]
60 to quickly validate identifiers:

>>>

filter[function, iterable]
0

In this case,

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 runs
filter[function, iterable]
58 on every string in
filter[function, iterable]
63. If the string is a valid Python identifier, then it’s included in the final result. Otherwise, the word is filtered out. Note that you need to use
filter[function, iterable]
57 to access
filter[function, iterable]
58 in the call to
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4.

Note: Besides

filter[function, iterable]
58,
filter[function, iterable]
57 provides a rich set of
filter[function, iterable]
69 that can be useful for filtering iterables of strings.

Finally, an interesting exercise might be to take the example further and check if the identifier is also a keyword. Go ahead and give it a try! Hint: you can use from the module.

Finding Palindrome Words

An exercise that often arises when you’re getting familiar with Python strings is to find palindrome words in a list of strings. A palindrome word reads the same backward as forward. Typical examples are “madam” and “racecar.”

To solve this problem, you’ll start by coding a predicate function that takes a string and checks if it reads the same in both directions, backward and forward. Here’s a possible implementation:

>>>

filter[function, iterable]
1

In

filter[function, iterable]
72, you first reverse the original
filter[function, iterable]
73 and store it in
filter[function, iterable]
74. Then you return the result of comparing both words for equality. In this case, you use
filter[function, iterable]
75 to prevent case-related differences. If you call the function with a palindrome word, then you get
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8. Otherwise, you get
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
9.

You already have a working predicate function to identify palindrome words. Here’s how you can use

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 to do the hard work:

>>>

filter[function, iterable]
2

Cool! Your combination of

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 and
filter[function, iterable]
72 works properly. It’s also concise, readable, and efficient. Good job!

Combining
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 With Other Functional Tools

So far, you’ve learned how to use

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 to run different filtering operations on iterables. In practice, you can combine
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 with other functional tools to perform many different tasks on iterables without using explicit loops. In the next two sections, you’ll learn the basics of using
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 along with
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4 and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6.

The Square of Even Numbers:
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4

Sometimes you need to take an iterable, process each of its items with a transformation function, and produce a new iterable with the resulting items. In that case, you can use

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4. The function has the following signature:

filter[function, iterable]
3

The arguments work like this:

  1. >>> objects = [0, 1, [], 4, 5, "", None, 8]
    
    >>> list[filter[None, objects]]
    [1, 4, 5, 8]
    
    7 holds the transformation function. This function should take as many arguments as iterables you pass into
    >>> def identity[x]:
    ...     return x
    ...
    
    >>> identity[42]
    42
    
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    >>> list[filter[identity, objects]]
    [1, 4, 5, 8]
    
    4.
  2. >>> numbers = [1, 3, 10, 45, 6, 50]
    
    >>> def extract_even[numbers]:
    ...     even_numbers = []
    ...     for number in numbers:
    ...         if number % 2 == 0:  # Filtering condition
    ...             even_numbers.append[number]
    ...     return even_numbers
    ...
    
    >>> extract_even[numbers]
    [10, 6, 50]
    
    4 holds a Python iterable. Note that you can provide several iterables to
    >>> def identity[x]:
    ...     return x
    ...
    
    >>> identity[42]
    42
    
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    >>> list[filter[identity, objects]]
    [1, 4, 5, 8]
    
    4, but that’s optional.

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4 applies
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
7 to each item in
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]
4 to transform it into a different value with additional features. Then
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4 yields each transformed item on demand.

To illustrate how you can use

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 along with
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4, say you need to compute the square value of all the even numbers in a given list. In that case, you can use
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 to extract the even numbers and then
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4 to calculate the square values:

>>>

filter[function, iterable]
4

First, you get the even numbers using

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 and
filter[function, iterable]
31 just like you’ve done so far. Then you call
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4 with a
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
3 function that takes a number and returns its square value. The call to
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4 applies the
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
3 function to each number in
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
08, so you get a list of square even numbers. The final example shows how to combine
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4 in a single expression.

Remove ads

The Sum of Even Numbers:
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6

Another functional programming tool in Python is

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6. Unlike
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4, which are still built-in functions,
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 was moved to the module. This function is useful when you need to apply a function to an iterable and reduce it to a single cumulative value. This kind of operation is commonly known as a reduction or folding.

The signature of

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 is like this:

filter[function, iterable]
5

Here’s what the arguments mean:

  1. >>> objects = [0, 1, [], 4, 5, "", None, 8]
    
    >>> list[filter[None, objects]]
    [1, 4, 5, 8]
    
    7 holds any Python callable that accepts two arguments and returns a single value.
  2. >>> numbers = [1, 3, 10, 45, 6, 50]
    
    >>> def extract_even[numbers]:
    ...     even_numbers = []
    ...     for number in numbers:
    ...         if number % 2 == 0:  # Filtering condition
    ...             even_numbers.append[number]
    ...     return even_numbers
    ...
    
    >>> extract_even[numbers]
    [10, 6, 50]
    
    4 holds any Python iterable.
  3. >>> numbers = [-2, -1, 0, 1, 2]
    
    >>> # Using a lambda function
    >>> positive_numbers = filter[lambda n: n > 0, numbers]
    >>> positive_numbers
    
    >>> list[positive_numbers]
    [1, 2]
    
    >>> # Using a user-defined function
    >>> def is_positive[n]:
    ...     return n > 0
    ...
    >>> list[filter[is_positive, numbers]]
    [1, 2]
    
    21 holds a value that serves as a starting point for the first partial computation or reduction. It’s an optional argument.

A call to

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 starts by applying
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
7 to the first two items in
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]
4. This way, it computes the first cumulative result, called an accumulator. Then
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 uses the accumulator and the third item in
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]
4 to compute the next cumulative result. The process continues until the function returns with a single value.

If you supply a value to

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
21, then
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 runs the first partial computation using
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
21 and the first item of
>>> numbers = [1, 3, 10, 45, 6, 50]

>>> def extract_even[numbers]:
...     even_numbers = []
...     for number in numbers:
...         if number % 2 == 0:  # Filtering condition
...             even_numbers.append[number]
...     return even_numbers
...

>>> extract_even[numbers]
[10, 6, 50]
4.

Here’s an example that combines

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 to cumulatively calculate the total sum of all the even numbers in a list:

>>>

filter[function, iterable]
6

Here, the first call to

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 computes the sum of all the even numbers that
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 provides. To do that,
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 uses a
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
3 function that adds two numbers at a time.

The final example shows how to chain

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 to produce the same result you got before.

Filtering Iterables With
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39

In

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
40, you’ll find a function called that does the inverse of
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4. It takes an iterable as argument and returns a new iterator that yields the items for which the decision function returns a false result. If you use
>>> import statistics as st
>>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]

>>> # The mean before removing outliers
>>> mean = st.mean[sample]
>>> mean
10.692307692307692

>>> stdev = st.stdev[sample]
>>> low = mean - 2 * stdev
>>> high = mean + 2 * stdev

>>> clean_sample = list[filter[lambda x: low > clean_sample
[10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]

>>> # The mean after removing outliers
>>> st.mean[clean_sample]
8.75
7 as the first argument to
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39, then you get the items that are falsy.

The point of having the

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39 function is to promote code reuse. If you already have a decision function in place, then you can use it with
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39 to get the rejected items. This saves you from coding an inverse decision function.

In the following sections, you’ll code some examples that show how you can take advantage of

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39 to reuse existing decision functions and continue doing some filtering.

Extracting Odd Numbers

You already coded a predicate function called

filter[function, iterable]
31 to check if a number is even or not. With that function and the help of
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39, you can build an iterator that yields odd numbers without having to code an
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
50 function:

>>>

filter[function, iterable]
7

In this example,

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39 returns an iterator that yields the odd numbers from the input iterator. Note that the call to
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39 is straightforward and readable.

Remove ads

Filtering Out NaN Values

Sometimes when you’re working with floating-point arithmetic, you can face the issue of having NaN [not a number] values. For example, say you’re calculating the mean of a sample of data that contains NaN values. If you use Python’s

filter[function, iterable]
49 module for this computation, then you get the following result:

>>>

filter[function, iterable]
8

In this example, the call to

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
54 returns
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
55, which isn’t the most informative value you can get. NaN values can have different origins. They can be due to invalid inputs, corrupted data, and so on. You should find the right strategy to deal with them in your applications. One alternative might be to remove them from your data.

The

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
56 module provides a convenient function called that can help you out with this problem. The function takes a number
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
58 as an argument and returns
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
8 if
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
58 is a NaN and
>>> objects = [0, 1, [], 4, 5, "", None, 8]

>>> list[filter[None, objects]]
[1, 4, 5, 8]
9 otherwise. You can use this function to provide the filtering criteria in a
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39 call:

>>>

filter[function, iterable]
9

Using

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
63 along with
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
39 allows you to exclude all the NaN values from the mean computation. Note that after the filtering, the call to
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
54 returns a value that provides a better description of your sample data.

Coding With Pythonic Style

Even though

>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
4,
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4, and
>>> def identity[x]:
...     return x
...

>>> identity[42]
42

>>> objects = [0, 1, [], 4, 5, "", None, 8]
>>> list[filter[identity, objects]]
[1, 4, 5, 8]
6 have been around for a long time in the Python ecosystem, list comprehensions and generator expressions have become strong and Pythonic competitors in almost every use case.

The functionality these functions provide is almost always more explicitly expressed using a generator expression or a list comprehension. In the following two sections, you’ll learn how to replace a call to

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 with a list comprehension or a generator expression. This replacement will make your code more Pythonic.

Replacing
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 With a List Comprehension

You can use the following pattern to quickly replace a call to

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 with an equivalent list comprehension:

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
0

In both cases, the final purpose is to create a list object. The list comprehension approach is more explicit than its equivalent

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 construct. A quick read through the comprehension reveals the iteration and also the filtering functionality in the
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
73 clause.

Using list comprehensions instead of

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 is probably the path most Python developers take nowadays. However, list comprehensions have some drawbacks compared to
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4. The most notable one is the lack of lazy evaluation. Also, when developers start reading code that uses
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4, they immediately know that the code is performing filtering operations. However, that’s not so evident in code that uses list comprehensions with the same goal.

A detail to notice when turning a

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 construct into a list comprehension is that if you pass
>>> import statistics as st
>>> sample = [10, 8, 10, 8, 2, 7, 9, 3, 34, 9, 5, 9, 25]

>>> # The mean before removing outliers
>>> mean = st.mean[sample]
>>> mean
10.692307692307692

>>> stdev = st.stdev[sample]
>>> low = mean - 2 * stdev
>>> high = mean + 2 * stdev

>>> clean_sample = list[filter[lambda x: low > clean_sample
[10, 8, 10, 8, 2, 7, 9, 3, 9, 5, 9, 25]

>>> # The mean after removing outliers
>>> st.mean[clean_sample]
8.75
7 to the first argument of
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4, then the equivalent list comprehension looks like this:

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
1

In this case, the

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
73 clause in the list comprehension tests
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
81 for its truth value. This test follows the standard Python rules about truth values you already saw.

Here’s an example of replacing

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 with a list comprehension to build a list of even numbers:

>>>

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
2

In this example, you can see that the list comprehension variant is more explicit. It reads almost like plain English. The list comprehension solution also avoids having to call

>>> import math

>>> def is_prime[n]:
...     if n >> is_prime[5]
True
>>> is_prime[12]
False
9 to build the final list.

Remove ads

Replacing
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 With a Generator Expression

The natural replacement for

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 is a generator expression. That’s because
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 returns an iterator that yields items on demand just like a generator expression does. Python iterators are known to be memory efficient. That’s why
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 now returns an iterator instead of a list.

Here’s how you can use generator expressions to write the example in the above section:

>>>

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
3

A generator expression is as efficient as a call to

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 in terms of memory consumption. Both tools return iterators that yield items on demand. Using either one might be a question of taste, convenience, or style. So, you’re in charge!

Conclusion

Python’s

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 allows you to perform filtering operations on iterables. This kind of operation consists of applying a Boolean function to the items in an iterable and keeping only those values for which the function returns a true result. In general, you can use
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 to process existing iterables and produce new iterables containing the values that you currently need.

In this tutorial, you learned how to:

  • Work with Python’s
    >>> numbers = [-2, -1, 0, 1, 2]
    
    >>> # Using a lambda function
    >>> positive_numbers = filter[lambda n: n > 0, numbers]
    >>> positive_numbers
    
    >>> list[positive_numbers]
    [1, 2]
    
    >>> # Using a user-defined function
    >>> def is_positive[n]:
    ...     return n > 0
    ...
    >>> list[filter[is_positive, numbers]]
    [1, 2]
    
    4
  • Use
    >>> numbers = [-2, -1, 0, 1, 2]
    
    >>> # Using a lambda function
    >>> positive_numbers = filter[lambda n: n > 0, numbers]
    >>> positive_numbers
    
    >>> list[positive_numbers]
    [1, 2]
    
    >>> # Using a user-defined function
    >>> def is_positive[n]:
    ...     return n > 0
    ...
    >>> list[filter[is_positive, numbers]]
    [1, 2]
    
    4 to process iterables and keep the values you need
  • Combine
    >>> numbers = [-2, -1, 0, 1, 2]
    
    >>> # Using a lambda function
    >>> positive_numbers = filter[lambda n: n > 0, numbers]
    >>> positive_numbers
    
    >>> list[positive_numbers]
    [1, 2]
    
    >>> # Using a user-defined function
    >>> def is_positive[n]:
    ...     return n > 0
    ...
    >>> list[filter[is_positive, numbers]]
    [1, 2]
    
    4 with
    >>> def identity[x]:
    ...     return x
    ...
    
    >>> identity[42]
    42
    
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    >>> list[filter[identity, objects]]
    [1, 4, 5, 8]
    
    4 and
    >>> def identity[x]:
    ...     return x
    ...
    
    >>> identity[42]
    42
    
    >>> objects = [0, 1, [], 4, 5, "", None, 8]
    >>> list[filter[identity, objects]]
    [1, 4, 5, 8]
    
    6 to approach different problems
  • Replace
    >>> numbers = [-2, -1, 0, 1, 2]
    
    >>> # Using a lambda function
    >>> positive_numbers = filter[lambda n: n > 0, numbers]
    >>> positive_numbers
    
    >>> list[positive_numbers]
    [1, 2]
    
    >>> # Using a user-defined function
    >>> def is_positive[n]:
    ...     return n > 0
    ...
    >>> list[filter[is_positive, numbers]]
    [1, 2]
    
    4 with list comprehensions and generator expressions

With this new knowledge, you can now use

>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 in your code to give it a functional style. You can also switch to a more Pythonic style and replace
>>> numbers = [-2, -1, 0, 1, 2]

>>> # Using a lambda function
>>> positive_numbers = filter[lambda n: n > 0, numbers]
>>> positive_numbers

>>> list[positive_numbers]
[1, 2]

>>> # Using a user-defined function
>>> def is_positive[n]:
...     return n > 0
...
>>> list[filter[is_positive, numbers]]
[1, 2]
4 with list comprehensions or .

Mark as Completed

🐍 Python Tricks 💌

Get a short & sweet Python Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.

Send Me Python Tricks »

About Leodanis Pozo Ramos

Leodanis is an industrial engineer who loves Python and software development. He's a self-taught Python developer with 6+ years of experience. He's an avid technical writer with a growing number of articles published on Real Python and other sites.

» More about Leodanis

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Aldren

Bartosz

David

Joanna

Jacob

Master Real-World Python Skills With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

Master Real-World Python Skills
With Unlimited Access to Real Python

Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:

Level Up Your Python Skills »

What Do You Think?

Rate this article:

Tweet Share Share Email

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. and get answers to common questions in our support portal.

Bài mới nhất

Chủ Đề