Comprehending comprehensions


#1



Hi folks
Do you know what is wrong with the code below?


m=[x for x in range(1,16)]
threes_and_fives=filter(lambda x:if x%3==0 or x%5==0,m)


#2

in this exercise , i think no need for filter and lambda ; create a list, threes_and_fives, that consists only of the numbers between 1 and 15 (inclusive) that are evenly divisible by 3 or 5.


#3

Yes,I did it as you say and was correct. but I want to know that what is the problem with this way?


#4

in lambda syntax no need for if , just write condition


#5

the code is tested against a certain test. Good solutions may fail to pass the test
this expression
threes_and_fives = [x for x in range(1,16) if (x % 3 == 0) or (x % 5 == 0)]
is one line of code, single function and provides the expected result,

using a "lambda filter" on a "list comprehension" provides the same result but do non pass the testing formula :slight_smile: