How to Filter Data in Python

Filter a list of dictionaries by exact key-value matches or numerical ranges using concise list comprehensions.

Easy Python 3.9+ Aug 9, 2026 Data pipelines & processing 12 views 0 copies

Python code

30 lines
Python 3.9+
from typing import List, Dict, Any


def filter_data(
    data: List[Dict[str, Any]], key: str, value: Any
) -> List[Dict[str, Any]]:
    """Return records where data[key] equals value."""
    return [record for record in data if record.get(key) == value]


def filter_by_range(
    data: List[Dict[str, Any]], key: str, low: float, high: float
) -> List[Dict[str, Any]]:
    """Return records where low <= data[key] <= high."""
    return [record for record in data if low <= record.get(key) <= high]


if __name__ == "__main__":
    dataset = [
        {"name": "Alice", "age": 30, "city": "NYC"},
        {"name": "Bob", "age": 25, "city": "LA"},
        {"name": "Carol", "age": 35, "city": "NYC"},
        {"name": "Dave", "age": 40, "city": "Chicago"},
    ]

    print("Filter by city == 'NYC':")
    print(filter_data(dataset, "city", "NYC"))

    print("\nFilter age between 25 and 35:")
    print(filter_by_range(dataset, "age", 25, 35))

Output

stdout
Filter by city == 'NYC':
[{'name': 'Alice', 'age': 30, 'city': 'NYC'}, {'name': 'Carol', 'age': 35, 'city': 'NYC'}]

Filter age between 25 and 35:
[{'name': 'Alice', 'age': 30, 'city': 'NYC'}, {'name': 'Bob', 'age': 25, 'city': 'LA'}, {'name': 'Carol', 'age': 35, 'city': 'NYC'}]

How it works

The filter_data function uses a list comprehension with record.get(key) == value to match exact field values. The record.get() method safely returns None when the key is missing, avoiding a KeyError. filter_by_range chains two comparisons — low <= value <= high — which Python evaluates as a single concise range check. Both functions return new lists, leaving the original dataset unchanged, which is ideal for immutable data pipelines. The __main__ guard lets the script run standalone while still being importable as a module.

Common mistakes

  • Using `record[key]` instead of `.get()` and crashing on missing keys
  • Forgetting that range filtering includes both endpoints (low and high)
  • Mutating the original list instead of returning a filtered copy

Variations

  1. Use a generator expression and convert to a list for large datasets
  2. Parameterize the comparison operator (e.g., '=' vs '<') for flexible queries

Real-world use cases

  • Filtering user records by a status field in an ETL job before loading into a warehouse.
  • Selecting sensor readings within a healthy temperature range for anomaly detection.
  • Filtering orders by location or price tier before sending to a recommendation engine.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Data pipelines & processing

Related tutorials and quizzes for this topic.