FAQ: Lambda Function Code Challenge - Contains A

This community-built FAQ covers the “Contains A” exercise from the lesson “Lambda Function Code Challenge”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Contains A

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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

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

I wanted to write a long hand version of the function before using lambda so I tried the following and failed, any ideas what’s wrong with it?

def contains_a(word):
  "a" in word
  return 
  

print contains_a("banana")
print contains_a("apple")
print contains_a("cherry")

also I don’t see why lambda is used, isn’t it better to use def as then it can be used in future whereas you don’t have this option with lambda

1 Like

Hello, @script0351949381.

When you paste code, especially python code, into a post it is helpful to those trying to help you if you use the </> button in the menu bar. If you go to a blank line, click the </> button, and then paste your code in the space indicated it will be properly formatted, so we can copy, and test it in our own environment.

"a" in word evaluates to True or False, but then what?
return will return the value that comes after it, but you don’t have one, so None will be returned.
If you want to return the result of "a" in word just put return in front of the expression:
return "a" in word

You might find this an interesting read: Lambda Functions in Python: What Are They Good For? – dbader.org

Happy coding!

2 Likes

My solution:

#Write your lambda function here
contains_a = lambda my_string: “a” in my_string
print contains_a(“banana”)
print contains_a(“apple”)
print contains_a(“cherry”)

2 Likes

the prints that test the function below are written without the parenthesis, they run in the web browser when you try it, but in visual studio i use, since is running python 3 they won’t work.

The syntax in this exercise may require updating, even if is something minor

#Write your lambda function here

contains_a = lambda palabra: ‘a’ in palabra

print contains_a(“banana”)

print contains_a(“apple”)

print contains_a(“cherry”)

thank you.
question, how do you know that the lambda function will identify the my_string input?
thnx

actually the print functions are already provided. Maybe it’s additional task to catch it but I honestly think it’s an honest mistake by Codecademy. Originally it looks like this, before you type anything
image

Here’s both ways of finding if ‘a’ is in word. The long (regular) function structure and lambda

# IT CAN BE DONE 2 WAYS #REGULAR LONG FUNCTION def contains_a_long(word): return 'a' in word # OR LAMBDA FUNCTION: contains_a_lambda = lambda word: 'a' in word #Write your lambda function here print(contains_a_long('bomba')) print(contains_a_lambda('turbulencje')) ## PRINT FUNCTIONS PROVIDED BELOW ## ## ARE MISSING PARENTHESES ## ## | ## \/ # print contains_a("banana") # print contains_a("apple") # print contains_a("cherry") # AFTER FIX SHOULD RETURN A PROPER RESULT print(contains_a_long("banana")) print(contains_a_long("apple")) print(contains_a_long("cherry")) print(contains_a_lambda("banana")) print(contains_a_lambda("apple")) print(contains_a_lambda("cherry"))

Hi. Rant incoming. I know some take it personally when I criticize CC but that is not my intention. I don’t want to offend anyone but this first exercise is a perfect example of my issues with CC. I’ve been doing python courses for over 18 months and have got precisely nowhere with it. Activities like this don’t help.

I read the lambdas informational and read the activity guidance and still got stuck on the first exercise. The guidance in the activity bears no resemblance to the solution. Looking at the help article which is the same as the informational I still have the same issue. Anyway, for this exercise, assuming I needed an if statement I tried,
contains_a = lambda word: return True if ‘a’ in word else return False

no. Syntax error

After similar failed attempts I bit the bullet and click view solution, which is:
contains_a = lambda word: “a” in word

where is that in the guidance/informational? How was I meant to guess that? Would it have killed you to put an exercise like the first one in the guidance, add_two = lambda my_input: my_input + 2 to get started?

It’s so demoralizing to constantly fail and be made to run before I can walk. Probably it’s just me but really if I was any good at this I wouldn’t be here would I? I’d just read the Python documentation and go with that. Likewise if I had a Computer Science degree. The activities need to be more beginner friendly and better reinforce previously learned concepts and fundamentals with a bit more copying/imitation of existing code and not random solutions.

Just to add, I do have over 10-year’s teaching experience so I’d like to think I know a little of what I’m talking about.

Rant over.

We cannot and will not speak for CC or the course author(s) or the even the course itself (please post a link to the exercise). What we can do is reiterate what @appylpye once said about lambda being a substitute for standalone functions: “We may as well stick to def.” (paraphrasing from memory)

That lambda is doing the work of a simple function, but its compactness is wasted, so I agree with Glenn.

def add_two (x):
    '''function to return 2 plus the input'''
    return x + 2

Notice we are able to include a docstring at the top of our function? We cannot do that with lambda. Our function is simple, and succinct and adheres to the standard syntax.

>>> help (add_two)
    function to return 2 plus the input
>>>

Basically, if a function is just doing some one-off thing then use the standard syntax. lambda is not improving anything in this case, and may just be muddying the code base with assignments, rather than function objects.

The example in the exercise is a twist on the lambda as in utilizing it as a predicate function, which can either return a boolean, or return any value that falls within the condition. In a one-off, again, it is kind of muddying up the water with assignment. First, let’s look at a standard function that does what that lambda does:

def contains_a (word):
    '''function to test for `a` in the input string'''
    return "a" in word

How does writing this as a lambda improve anything?

contains_a = lambda word: "a" in word

In both cases we invoke the function the same way.

print (contains_a ("mountain"))    #  True

One does not see how brevity is of any real value over the standard form of the function definition, in this instance. I would stick with the function.

What the lesson might not have gotten into is the where a lambda is a good fit in many situations where an outer function is iterating over the inner function any number of times. These are instances where if a standalone function is just taking up space, and the inner function is never called upon anywhere else in the program, then an anonymous lambda is the right tool.

It might be necessary to pause this discussion until you are in the unit on iterators. Let us know when you get to that stage and we can pick this up then. Otherwise, stick with ordinary functions and set lambda to the side, for the time. It’s not an important feature in a run of the mill program, as I think Glenn was basically saying.

1 Like

This is where the exercise occured:

https://www.codecademy.com/journeys/data-scientist-ml/paths/dsmlcj-22-data-science-foundations-ii/tracks/dsmlcj-22-pandas-for-data-science/modules/dsf-lambda-fundations-for-pandas-320341d8-ac60-4f50-b711-33008b402b88/lessons/lambda/exercises/contains-a

To be fair, it is the first exercise to teach the learner how to use lambda function in operation - so an introduction essentially. It’s beyond the iterators at this point and you would expect the learner to start somewhere even if the exercise may not be the best demonstrator of why use lambda.

Unfortunately, I think the poster above needed to take a closer look at the informational and take a bit more time reviewing the foundationals. It’s meant to be a simple exercise to introduce how to write lambda, and in the infomational it states that return cannot be used, which is the root of the poster’s issue, not the guess work.