Pages

Thursday, November 8, 2012

python map and filter

Python's functional programming has two important method - map and filter. map has an input of a list, and iterates over the elements by applying a method.

filter uses the method given to filter out the elements that returns true when applying the method.

This is an example.
pair = \
[
{'group': 'b', 'method': 'test'},
{'group': 'a', 'method': 'test'}
]

print map(lambda x: x['group'] == 'a', pair)
print filter(lambda x: x['group'] == 'a', pair)
This is the result.
[False, True]
[{'group': 'a', 'method': 'test'}]

No comments:

Post a Comment