[Tutor] Difference between filter and map

Chris Calloway cbc at unc.edu
Tue Jan 23 16:48:21 CET 2007

vanam wrote:
> ya i am sure about that i am using python editor which has python 
> intrepreter attached to it i got the same output for both filter and map
> def squ(n):
>    y = n*n
>   print y
> filter(y,range(3))->0  1 4
> map(y,range(3))->0 1 4

You are not printing the result of either the filter or map function 
here. You have the print statement embedded in squ. In fact you wouldn't 
print anything but a NameError exception here because you haven't passed 
filter or map a function, just an identifier which isn't in their scope:

 >>> def squ(n):
...    y = n*n
...    print y
 >>> filter(y, range(3))
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
NameError: name 'y' is not defined

Also not, the function squ as defined here always returns None, so it is 
useless as either a filtering or mapping function. Printing a value is 
not the same as returning a value.


Chris Calloway
office: 332 Chapman Hall   phone: (919) 962-4323
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599

More information about the Tutor mailing list