On Tue, 23 Jan 2007, vanam wrote:
> i want to know the difference between filter(function,sequence) and > map(function,sequence). Hi Vanam, They may both take functions as input, but the intention of the functions is different. In the case of filter(), the input function is used to cull the good elements of the sequence out. ############################################################################ >>> def starts_with_ab(word): ... return word[:2] == 'ab' ... >>> filter(starts_with_ab, ["abracadabra", "open sesame", "abraham lincoln"] ... ) ['abracadabra', 'abraham lincoln'] ############################################################################ > i am getting the same results for instance > def squ(x): > return x*x > filter(squ,range(1,3))->1,4(output) > map(squ,range(1,3)->1,4(output) This doesn't look right. Please copy and paste exactly what you typed, and exactly what the program outputted. You need to do this carefully or we can't reproduce what you see. In fact, concretely, when you reply to Kent, you show a different definition of squ(): ########### def squ(n): y = n*n print y ########### Be more careful next time. In this case, squ() isn't even returning back a value, so you can't expect good things to come out of filter() and map(): filter and map depend on the input function actually giving values back. What you're seeing on screen is the "side-effects" of calling squ() on every element. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor