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.
Python code
18 linesfrom 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
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
- Use `functools.reduce` with a lambda that calls the function directly
- 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
More from Functions & basics
- Add Type Hints to Function Parameters and Return in Python easy
- Benchmark list append vs comprehension in Python easy
- Build a Context Manager in Python with contextlib.contextmanager easy
- Build a Progress Callback Function for Loops in Python easy
- Cache expensive function with lru_cache in Python easy
- Calculate Time Difference Across Time Zones in Python easy
Keep learning
Related tutorials and quizzes for this topic.