14 Try it! Why do i get <built-in function filter> message


#1

https://www.codecademy.com/courses/learn-python/lessons/advanced-topics-in-python/exercises/try-it?action=lesson_resume

Isn’t this the same as:


 print filter(lambda x: x >= 30 and x <= 70, squares)

as



filter(lambda x: x >= 30 and x <= 70, squares)

print filter


Why is the outcome different ?


#2

You have not set a variable to the value of the filter. Which it is why you are getting the error and why the above two statements are not equivalent.

To clarify:

f = filter(lambda x: x >= 30 and x <= 70, squares)
print f

Will result in the intended output (and I think directly adresses your question).

However, it would better to do it the first way.


#3

print filter will simply print the function object, if you add parentheses you call the function (which will execute the function)


#4

I agree, the first way is better. I was trying to get a better understanding. Thanks to both of you!


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.