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

Stefan Vaziri
Stefan Vaziri
17,453 Points

Using the if, else statement with reduce() and lambda()

I think I got the beginning portion with reduce and lambda right. I think you need to use the if statement here but I'm not sure how to incorporate that and pick the longest string with it.

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 string: if string[len(string)] >:

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You are very much on the right path. I reran the challenge and paused at the same point: the function passed to reduce must process two arguments. How to compare two string with a lambda function. The key is to provide two arguments to the lambda then return the longest:

# take two arguments, compare lengths, return longest
lambda string1, string2: string1 if len(string1) > len(string2) else string2, strings

# using this lambda in the reduce() along with the 'strings' argument:
longest = reduce(lambda string1, string2: string1 if len(string1) > len(string2) else string2, strings)
Eray Ates
Eray Ates
14,292 Points

I used that if else statement for calculating total weight of my number with my second count list. I am not sure it's good for this problem but you can think like that. First element get result of first calculation so we should use integer or what kind of that for later calculation.

from functools import reduce
myNumbers = [10, 20, 30, 40, 50]
myCount = [1, 2, 3, 4, 5]
totalW = reduce(lambda x, y : x[0]*x[1] + y[0]*y[1] if type(x) == tuple else x + y[0]*y[1], zip(myNumbers, myCount))
print(totalW) #550

The key is 'find the longest string and store the string' ...NOT find the longest length and store the length in 'longest'.

longest = reduce(lambda x, y: len(x) if len(x) > len(y) else len(y), strings)

This wasn't passing as a result.

Also, watch your spelling of lambda or you'll be like me and keep spelling it lamdba. Can't see the difference. Yeah, neither could I...