Advanced Topics in Python


#1

Hi, In this lesson(17-12) the description is telling that

lambda x: x % 3 == 0
Is the same as

def by_three(x):
    return x % 3 == 0

and if i try

my_list = range(16)
print filter(lambda x: x % 3 == 0, my_list)

it is working perfectly fine but if i instead of using lambda, i used like general functions, like below

    my_list = range(16)
    def filter(x):
        return x % 3 == 0

    print filter(my_list)

it is throwing error(of course i predicted the error) and i wonder, the description is wrong ? or my understanding of functions isc Please explain how can possibly work with lambda expressions ?


#2

Hi,

filter is a build in function in Python. You don't have to define it. Instead define a function that returns True or False and use filter with it.

I think this is what you were trying to accomplish:

def by_three(x):
    return x % 3 == 0

print filter(by_three, range(16))

#3

Thank you very much for your answer @eveat