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

Lambda 2. Code picked the right sentence but claims I didn't use reduce and a lambda.

when I print longest in workspaces it has set longest to the My grandmother sentence so it is doing what I want

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.",
]

longest = reduce(lambda sent1, sent2: sent1 if sent1 > sent2 else sent2, [len(b) for b in strings])

3 Answers

Steven Parker
Steven Parker
229,744 Points

The error message may be a bit misleading.

But while your solution finds the length of the longest string, the instructions ask you to find the longest string itself.

Steven Parker
Steven Parker
229,744 Points

And apparently, the challenge doesn't like list comprehensions. But you won't need one for this challenge.

i tried it this way: longest = reduce(lambda sent1, sent2: sent1 if len(sent1) > len(sent2) else sent2, [b for b in strings]) and when i print longest i still print the grandma sentence but it doesn't pass

as in print(longest) produces "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."

also tried removing the length call all together and still ended with the same result

Steven Parker
Steven Parker
229,744 Points

While your code is meeting the requirements now, the challenge apparently doesn't like the list comprehension.

But since it doesn't do anything now anyway, just replace it with "strings" and you'll pass.

Well that worked thank you