473,241 Members | 1,730 Online

# filter vs map vs reduce functions

I am not clear with concepts of filter vs map vs reduce functionsin list data structure.Can anyone explain them with examples.when and where they can be used??

Dec 26 '07 #1
2 4772
bvdet
2,851 Expert Mod 2GB
I am not clear with concepts of filter vs map vs reduce functionsin list data structure.Can anyone explain them with examples.when and where they can be used??

Here are examples of each:
Expand|Select|Wrap|Line Numbers
1. >>> def imp_to_mm(inches):
2. ...     return inches * 25.4
3. ...
4. >>> inchesList = [1,2,12,24,36]
5. >>> map(imp_to_mm, inchesList)
6. [25.399999999999999, 50.799999999999997, 304.79999999999995, 609.59999999999991, 914.39999999999998]
7. >>> def is_str(item):
8. ...     if isinstance(item, str):
9. ...         return True
10. ...     return False
11. ...
12. >>> filter(is_str, ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'])
13. ['a', 'BBB', 'A string']
14. >>> reduce(lambda x,y: x+y, [1,2,3,4,5,6,7,8,9,10])
15. 55
16. >>>
map() applies a function to each element of a list and returns a new list. filter() filters the elements of a list with a filter function (the function should return True or False) and returns a new list consisting of the elements of the argument list that evaluate True. I prefer to use a list comprehension.
Expand|Select|Wrap|Line Numbers
1. >>> [i for i in ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'] if isinstance(i, str)]
reduce() collects information from a sequence and returns a single value. It works by applying the argument function to the first two elements of the sequence. The value is then combined with the third element in the sequence and so on.
Dec 26 '07 #2
Here are examples of each:
Expand|Select|Wrap|Line Numbers
1. >>> def imp_to_mm(inches):
2. ...     return inches * 25.4
3. ...
4. >>> inchesList = [1,2,12,24,36]
5. >>> map(imp_to_mm, inchesList)
6. [25.399999999999999, 50.799999999999997, 304.79999999999995, 609.59999999999991, 914.39999999999998]
7. >>> def is_str(item):
8. ...     if isinstance(item, str):
9. ...         return True
10. ...     return False
11. ...
12. >>> filter(is_str, ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'])
13. ['a', 'BBB', 'A string']
14. >>> reduce(lambda x,y: x+y, [1,2,3,4,5,6,7,8,9,10])
15. 55
16. >>>
map() applies a function to each element of a list and returns a new list. filter() filters the elements of a list with a filter function (the function should return True or False) and returns a new list consisting of the elements of the argument list that evaluate True. I prefer to use a list comprehension.
Expand|Select|Wrap|Line Numbers
1. >>> [i for i in ['a', 1, 12.5, [3,4,5], 'BBB', inchesList, 'A string'] if isinstance(i, str)]
reduce() collects information from a sequence and returns a single value. It works by applying the argument function to the first two elements of the sequence. The value is then combined with the third element in the sequence and so on.

I know this forum will never disappoint and will clarify every minute doubts without criticizing.
Thank u so much
Dec 27 '07 #3