How to Pipe Data Through a List of Transform Functions in Python

Applies a sequence of functions to an initial value using functools.reduce, creating a reusable pipe utility.

Easy Python 3.9+ Aug 9, 2026 Functions & basics 13 views 0 copies

Python code

18 lines
Python 3.9+
from functools import reduce

def pipe(data, *transforms):
    return reduce(lambda value, func: func(value), transforms, data)

def double(x):
    return x * 2

def add_one(x):
    return x + 1

def to_string(x):
    return f"Result: {x}"

if __name__ == "__main__":
    initial = 5
    result = pipe(initial, double, add_one, to_string)
    print(result)

Output

stdout
Result: 11

How it works

The pipe function uses functools.reduce to sequentially apply each transform function to the data. The initial value data is passed as the third argument to reduce, and the lambda takes the current value and the next function, applying it immediately. Because functions are first-class objects in Python, they can be passed as arguments just like any other value. The transforms are applied left to right: double(5) becomes 10, add_one(10) becomes 11, and to_string(11) produces the final string. This pattern is equivalent to to_string(add_one(double(initial))) but is more readable and extensible.

Common mistakes

  • Forgetting to pass the initial value as the third argument to reduce, which would raise a TypeError
  • Confusing the order of transforms — it evaluates left to right, not right to left
  • Assuming the transforms must be a list instead of accepting *args
  • Not handling empty transforms list, which would return the data unchanged (which is correct for pipe)

Variations

  1. Use `functools.reduce` with a lambda that calls the function directly
  2. Use a loop to apply each function in sequence without reduce

Real-world use cases

  • Building data preprocessing pipelines for ML features where each transform is a function.
  • Applying a series of validation and cleaning steps to user input before storage.
  • Chaining middleware-style transformations in an ETL job before writing to a database.

Sponsored

Run this sample

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

Open editor

More from Functions & basics

Related tutorials and quizzes for this topic.