471,354 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

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??

Thanks in advance
Dec 26 '07 #1
2 4661
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??

Thanks in advance
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

226 posts views Thread by Stephen C. Waterbury | last post: by
4 posts views Thread by Ben | last post: by
5 posts views Thread by Logan | last post: by
11 posts views Thread by Xah Lee | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.