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.
Python code
18 linesdef 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
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
- Use a generator expression with `sum(1 for x in data if condition)` to count matches without building a list.
- 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
More from Comprehensions & generators
- Batch Rows in Chunks with a Generator in Python easy
- Build a Generator Pipeline in Python: Filter Then Map medium
- Build a lazy generator to read file lines in Python easy
- Chunk an Iterable into Batches with a Generator in Python easy
- Convert Data in Python with Comprehensions and Generators easy
- Count Data in Python with Comprehensions and Generators easy
Keep learning
Related tutorials and quizzes for this topic.