FUNCTIONALLY ORIENTED PROGRAMMING IN PYTHON
Python supports methods (called iterators in Python 3), such as filter(), map(), and reduce() that are very useful when you need to iterate over the items in a list, create a dictionary, or extract a subset of a list. These iterators are discussed in the following subsections.
The Python filter() Function
The filter function enables you to extract a subset of values based on conditional logic. The following example returns a list of odd numbers between 0 and 15 inclusive that are multiples of 3:
>>> range(0,15)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
>>> def f(x): return x % 2 != 0 and x % 3 == 0
...
>>> filter(f, range(0, 15))
[3, 9]
>>>
The Python map() Function
The Python map() command is a built-in function that applies a function to each item in an iterable. The map(func, seq) calls func(item), where item is an element in a sequence seq, and returns a list of the return values.
Listing...