473,405 Members | 2,154 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,405 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 4779
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.