Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Functional Python The Lambda Lambada Lambda 2

Andy McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy McDonald
Python Development Techdegree Graduate 13,801 Points

give_me_this....?????????????????????

I get that being able to discover unknown elements in python is valuable but its really annoying when you get quizzed on something you have never seen before and-let me tell you-this one is an absolute GEM! I just got done hearing that return cant be used in a lambda function and now I'm supposed to figure out what kind of syntax is being hinted at with "give_me_this" or "give_me_that". Obviously I need help. I would bet thousands of dollars that anyone encountering this for the first time would have zero clue on how to proceed.

This is especially difficult because I feel there is a need for variable assignment to complete this especially for saving the longest string and replacing it with anything that ends up being longer. If I had any idea what "give_me_this" actually is, I might be able to conceptualize a way to solve this quiz but I have absolutely no clue.

strings.py
from functools import reduce

strings = [
    "Do not take life too seriously. You will never get out of it alive.",
    "My fake plants died because I did not pretend to water them.",
    "A day without sunshine is like, you know, night.",
    "Get your facts first, then you can distort them as you please.",
    "My grandmother started walking five miles a day when she was sixty. She's ninety-seven know and we don't know where she is.",
    "Life is hard. After all, it kills you.",
    "All my life, I always wanted to be somebody. Now I see that I should have been more specific.",
    "Everyone's like, 'overnight sensation.' It's not overnight. It's years of hard work.",
]

from functools import reduce

strings = [
    "Do not take life too seriously. You will never get out of it alive.",
    "My fake plants died because I did not pretend to water them.",
    "A day without sunshine is like, you know, night.",
    "Get your facts first, then you can distort them as you please.",
    "My grandmother started walking five miles a day when she was sixty. She's ninety-seven know and we don't know where she is.",
    "Life is hard. After all, it kills you.",
    "All my life, I always wanted to be somebody. Now I see that I should have been more specific.",
    "Everyone's like, 'overnight sensation.' It's not overnight. It's years of hard work.",
]

longest = reduce(lambda x, y: give_me_this if x > y else give_me_that, [len(string) for string in strings])

1 Answer

In the statement give_me_this if this_thing > that_thing else give_me_that, give_me_this and give_me_that are the the code that is run based on the result of the if statement. If the if statement is true, give_me_this is run and not give_me_that. If the if statement is false, give_me_that is run and not give_me_this.

print("Case 1") if 1 > 0 else print("Case 2") # prints "Case 1"
print("Case 1") if 0 > 1 else print("Case 2") # prints "Case 2"
Andy McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andy McDonald
Python Development Techdegree Graduate 13,801 Points

With my limited understanding I see the only possible way of returning the longest string is thorough variable assignment. I feel like I would have to save the longest string to x or y and then recursively run the function again comparing the assigned variable to the next string to see which is longest because an if greater than statement is only going to return true or false. I can't tell it to return the greater variable because i guess that's not okay with a lambda function and its asking me to assign the longest string to 'longest' and not print the function.

Lambdas have return values; they just don't use the return keyword.

You could do something like lambda x, y: x if len(x) > len(y) else y, which returns x if len(x) > len(y) and y if len(x) <= len(y), and pass that as the first argument for reduce. The reduce function will apply the function passed into it as its first argument to all the list elements passed to it as its second argument.