How to Use Comprehensions and Generators to Check Data in Python
A beginner-friendly helper that filters numeric values, computes squares and cubes with comprehensions and a generator, and returns a summary dictionary.
Python code
20 linesdef check_data(iterable):
"""Return a summary of numeric data using comprehensions and a generator."""
values = [item for item in iterable if isinstance(item, (int, float))]
squares = [x ** 2 for x in values if x > 0]
cubes = (x ** 3 for x in values if x > 0)
cube_list = list(cubes)
return {
"count": len(values),
"sum": sum(values),
"positive_squares": squares,
"positive_cubes": cube_list,
"average": sum(values) / len(values) if values else 0,
}
if __name__ == "__main__":
sample_data = [1, -2, 3.5, "abc", None, 4, 0, -1, 6]
result = check_data(sample_data)
for key, value in result.items():
print(f"{key}: {value}")
Output
count: 7
sum: 11.5
positive_squares: [1, 12.25, 16, 36]
positive_cubes: [1, 42.875, 64, 216]
average: 1.6428571428571428
How it works
The function filters non-numeric items using a list comprehension with isinstance, so strings and None are ignored. Squares are computed eagerly into a list, while cubes use a generator expression that produces values lazily—converting to a list realizes them. The final dictionary groups the results neatly, and the average is guarded against an empty list to avoid division by zero. Using comprehensions keeps the code concise and readable compared to manual loops.
Common mistakes
- Forgetting to handle empty input, causing ZeroDivisionError when computing average
- Using `isinstance(item, (int, float))` without considering booleans, which are ints
- Confusing list comprehensions (eager) with generator expressions (lazy)
- Filtering zero incorrectly when positive/negative logic is intended
Variations
- Use `statistics.mean(values)` from the stdlib to compute average.
- Replace squares list with a generator and consume it with `sum()` or `map()`.
Real-world use cases
- Summarizing numeric sensor readings while ignoring metadata strings in a log file.
- Preprocessing a mixed-type dataset column to extract only numbers for statistical analysis.
- Building a quick data-quality report before feeding cleaned data into a machine-learning pipeline.
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.