473,241 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 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??

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

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

Similar topics

226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
5
by: Logan | last post by:
I read in PEP 279 the following comment by GvR: "filter and map should die and be subsumed into list comprehensions, not grow more variants." Actually, I am using 'filter' and 'map' a lot in...
11
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © # Python © © # the "filter" function can be used to © # reduce a list such that unwanted © # elements are removed. © # example: © © def even(n): return n % 2 == 0...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.