---
title: filter()
url: https://docs.vultr.com/python/built-in/filter
description: Filter Collection Items
publish_date: 2024-11-22T20:20:00.080899Z
last_updated: 2024-11-22T20:20:00.080899Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/1021/ceab7340-336f-486d-8404-49c230be1109.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Python filter() - Filter Collection Items

## Introduction
The `filter()` function in Python provides a convenient way for filtering collections through a function that tests each element in the iterable to be true or false. This function is fundamental in data processing and transformation tasks, allowing developers to quickly isolate items that meet specific conditions.

In this article, you will learn how to effectively leverage the `filter()` function to refine collections in Python. Explore practical examples of filtering lists, tuples, and dictionaries to understand how `filter()` can greatly enhance data manipulation capabilities in your code.

## Filtering Lists

### Filter Numbers by Conditions
1. Define a list of numbers.
1. Create a function that specifies the filtering condition, such as retaining even numbers.
1. Use the `filter()` function to apply this condition.

    ```python
    def is_even(num):
        return num % 2 == 0

    numbers = [1, 2, 3, 4, 5, 6]
    even_numbers = filter(is_even, numbers)
    print(list(even_numbers))
    ```

    This code defines a function `is_even()` that checks if a number is even. The `filter()` function then uses this condition to sift through the `numbers` list, returning only even numbers.

### Using Lambda Functions for Inline Filtering
1. Specify your list of data.
1. Apply the `filter()` function with a lambda function for simpler conditions.

    ```python
    numbers = [1, 2, 3, 4, 5, 6]
    even_numbers = filter(lambda x: x % 2 == 0, numbers)
    print(list(even_numbers))
    ```

    The lambda function `lambda x: x % 2 == 0` acts as a quick inline function that checks for even numbers, simplifying the code without needing a defined function like `is_even()`.

## Filtering Tuples

### Select Items from a Tuple
1. Create a tuple containing various types of elements.
1. Develop a function that identifies the desired type, such as strings.
1. Apply `filter()` to isolate these elements.

    ```python
    def is_string(element):
        return isinstance(element, str)

    mixed_tuple = (1, 'apple', 2, 'banana', 3.5)
    string_elements = filter(is_string, mixed_tuple)
    print(tuple(string_elements))
    ```

    Here, `is_string()` checks each element to see if it is a string. This is particularly useful for data type-specific operations within tuples.

## Filtering Dictionaries

### Extract Entries Based on Conditions
1. Initialize a dictionary.
1. Utilize a function to determine which key-value pairs to keep.
1. Use the `filter()` function combined with `dict.items()` to filter the dictionary.

    ```python
    def has_positive_value(item):
        key, value = item
        return value > 0

    data_dict = {'a': 10, 'b': -5, 'c': 0, 'd': 15}
    positive_value_dict = filter(has_positive_value, data_dict.items())
    print(dict(positive_value_dict))
    ```

    The function `has_positive_value()` assesses each item, and only key-value pairs with positive values pass through the `filter()`. Using `dict.items()` converts the dictionary into a list of tuples, which `filter()` can process effectively.

## Conclusion
Python's `filter()` function is a versatile tool for filtering elements in a collection based on specific test functions. Whether dealing with lists, tuples, or dictionaries, `filter()` can significantly streamline the process of data extraction and refinement. By integrating the examples and techniques discussed, you can simplify and optimize your data filtering tasks in Python scripts, enhancing your code's readability and efficiency.