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.

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

Python code

20 lines
Python 3.9+
def 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

stdout
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

  1. Use `statistics.mean(values)` from the stdlib to compute average.
  2. 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

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.