[Tutor] Difference between filter and map

Kent Johnson kent37 at tds.net
Wed Jan 24 11:56:50 CET 2007


vanam wrote:
> Yes i did a mistake in expressing my problem below are the instances of 
> the script and its corresponding output,for each instance its giving 
> contrasting result i want explanation for that

This has pretty much been explained already. Do you have some question 
with the explanation?

> [1]:def squ(n):
>            return n*n
>      filter(squ,range(3))---->output is not seen on the interpreter
>      map(squ,range(3))----->output  not seen on the interpreter
> print filter(squ,range(3))----->output is [1,2]
> print map(squ,range(3))------>output is [0,1,4]
> 
> [2]:def squ(n):
>           y = n*n
>           print y
>       filter(squ,range(3))-->Below is the output

Note that in a script, the results of function calls are not printed 
unless you explicitly ask for it with a print statement. So the output 
here is from the "print y" in squ(), it is not the result of filter().

>       0
>       1
>       4
>       map(squ,range(3))-->Below is the output
>       0
>       1
>       4

Again, this is just the output from "print y"

>       print filter(squ,range(3))--->Below is the output
>       0
>       1
>       4

This is the result from filter():
>       []
>       print map(squ,range(3))-->Below is the output
>       0
>       1
>       4

This is the result from map():
>       [None,None,None]
> I want to know why in each case its giving different results and diff 
> between filter and map

Please reread the previous replies, all of this is explained.

Kent



More information about the Tutor mailing list