Not seeing your question? It may still have been asked before – try () in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post ().
Other FAQs
The following are links to additional questions that our community has asked about this exercise:
This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!
If however we call the my_list variable (my_list = range(16) into either of these functions, the conventional function (def by_three(x)) throws an error.
So is Codecademy incorrect here? If it’s the same then they should both print [0,3,6,9,12,15] right?
I went on to play around with def by_three(x) a bit more and a few more things have confused me.
So this prints the same numbers as the lamda function above:
def by_three(x):
for i in x:
if i % 3 == 0:
print i
by_three(my_list)
This next example however just prints 0. Why?
for i in x:
if i % 3 == 0:
return i
print by_three(my_list)
Thank you for your help. I really should understand these differences by now but apparently I don’t!
I’ve only been able to glance at these lessons but the instructions state that just lambda x: x% 3 == 0 was equivalent to the function definition.
Adding the filter function and passing it that lambda object and your iterable is quite different. The correct equivalence would be- filter(by_three, my_list).
Your loop will not perform as expected because the return statement will execute on 0 and the function will exit (no other numbers would be checked). The simplest way would be to use filter as expected but you could perhaps look at generator functions (which I think would be closer to the logic you’re using) though they may not have come up in lessons. Best to stick with what you’re at and mark it as something to look at in the future.
As an additional it’d be worth looking at the following FAQ which describes how to format code which would make it much easier to understand as it respects indentations and such.