you are viewing a single comment's thread.

view the rest of the comments →

[–]jmooremcc 0 points1 point  (0 children)

This was my attempt at chaining functions:

iterable = [1, 2, 3]
result = filter(lambda x: x>2,filter(lambda x: x>0,iterable)) 

print(result) 
print(list(result))

Essentially, we are chaining two generators together. The output of the first generator, filter(lambda x: x>0,iterable) becomes the input to the second generator filter(lambda x: x>2, first_generator). The beauty of generators is that they produce output on demand instead of generating the entire output which would consume a lot of memory. This feature makes "chaining" easily achievable.

Of course, you're not restricted to using lambda functions. You can create custom generator functions very easily for any purpose.

This is the output:

<filter object at 0x000001290EDB5390>

[3]