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 trialBartlomiej Damek
12,132 PointsUse reduce() and a lambda to find the longest string in strings. Save this value in the variable
Can you help me?
Bummer! Did you use reduce and a lambda?
print(longest) 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.
???
Regards Bartek
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.",
]
f = lambda a,b: a if (len(a) > len(b)) else b
longest = reduce(f, strings)
4 Answers
Julian Garcia
18,380 PointsI just include your lambda in reduce call.
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 a,b: a if (len(a) > len(b)) else b, strings)
Hope this helps.
Bartlomiej Damek
12,132 Pointsit works :)
Thanks
Vincent Zamora
3,872 PointsFor this one "longest = reduce(lambda a, b : a if a > b else b, strings)" works as well. I confirmed it in Pycharm. Notice I didn't use len(a). Is this acceptable, do we have to use "len"? Both seem to work.
Chris Hummel
13,960 PointsI've been having a really hard time with these challenges. It seems like in the videos we're given super simple examples of how this would be used, but then in the challenges a more complex way of using it is presented. ie when did we go over the syntax for using if statements with lambda and reduce? This isn't the first time I've noticed this going through the Python tracks either. I spent quite a bit of time before finding this thread, and my issue wasn't logic, but rather syntax. Just feel like this course/track could use a little more in depth explanations and examples to help grasp the concepts a little better. I hope this can be seen as constructive as I do really enjoy the courses on here, I just think this is an area that could be improved. Thanks!