FAQ: Learn Python - Advanced Topics in Python - Anonymous Functions


This community-built FAQ covers the “Anonymous Functions” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Anonymous Functions:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

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!

Hi there. I’d just like to say that the definition of functional programming presented in this lesson is incorrect.
A few references:



So the instructions for this exercise say that:

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

is the same as:

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

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

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!

Kind regards,

Hi @py2632679872 .

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.

Thanks tgrtim,
That helps to clarify things a bit. I’ll be sure to check out those FAQs!
Thanks again,

For anyone else really confused on what exactly filter does and how this all works this website gives a good explanation Python filter()