OptimazeFunction
Please optimize the following Python function for performance and memory usage, without changing its external behavior. Requirements: Justify any algorithmic improvements Ensure the code remains readable and testable Use idiomatic Python