How to Validate Data with Python Comprehensions and Generators

Use list, generator, and dictionary comprehensions to filter and transform data for quick validation in Python.

Easy Python 3.9+ Aug 9, 2026 Comprehensions & generators 14 views 0 copies

Python code

18 lines
Python 3.9+
def validate_integer(data):
    return [item for item in data if isinstance(item, int)]

def validate_positive(numbers):
    return (num for num in numbers if num > 0)

def validate_string_lengths(data, min_length=3):
    return {item: len(item) for item in data if isinstance(item, str) and len(item) >= min_length}

if __name__ == "__main__":
    mixed = [1, "hello", -5, "hi", 10, 0, "python", 3.14]
    valid_ints = validate_integer(mixed)
    positive_nums = list(validate_positive([-2, 5, 0, 8, -1, 3]))
    string_lengths = validate_string_lengths(mixed)

    print("Valid integers:", valid_ints)
    print("Positive numbers:", positive_nums)
    print("String lengths:", string_lengths)

Output

stdout
Valid integers: [1, -5, 10, 0]
Positive numbers: [5, 8, 3]
String lengths: {'hello': 5, 'python': 6}

How it works

List comprehensions build a new list by applying an expression to each item that passes the filter. Generator expressions are lazy — they produce values on demand, which saves memory for large inputs but require list() or a loop to see results. Dictionary comprehensions create a dict with key–value pairs, here mapping each string to its length. Using isinstance(item, int) correctly identifies integers but excludes booleans (since bool is a subclass of int in Python).

Common mistakes

  • Using `int(item)` instead of `isinstance(item, int)` will convert floats or strings, not filter them.
  • Forgetting to wrap a generator expression with `list()` before printing or iterating multiple times.
  • Assuming booleans are excluded when using `isinstance(item, int)` — they are included because `bool` subclasses `int`.

Variations

  1. Use a generator expression with `sum(1 for x in data if condition)` to count matches without building a list.
  2. Replace the dictionary comprehension with `dict((item, len(item)) for item in data if ...)` for older Python versions.

Real-world use cases

  • Cleaning user input by filtering out invalid types before processing form submissions.
  • Building lookup tables that map product IDs to their lengths for quick validation in an API response.
  • Streaming log lines through a generator to extract only error entries without loading the whole file into memory.

Sponsored

Run this sample

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

Open editor

More from Comprehensions & generators

Related tutorials and quizzes for this topic.