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

Use a lambda and filter(), create a variable high_cal with items in meals where the "calories" > 1000

Link to Objective: http://teamtreehouse.com/library/functional-python/the-lambda-lambada/lambda-2

I've watch the Lambda video time and again.

I've downloaded the zip and looked at the "stock.py" file, but it deals with

json so it really doesn't match up with any of the objectives (that don't mention json at all)


I don't know...all of Kenneth Love's code examples and objective questions seem so obtuse.

The don't really google well.

I didn't have any difficulties with the python in kat chuang's course:

http://teamtreehouse.com/library/data-science-basics


Another thing:

Did you ever notice that the "preview" (and answer analyzer) for the

python objectives is really awful (either offering nothing or

no really helpful debugging suggestions)?


Anyway, here are some of my attempts (tries)

that all got generic "Bummer!" errors:

high_cal = meals.filter(lambda x: x[1] > 1000)

high_cal = filter( lambda x:  x[1] > 1000, meals)

high_cal = filter( lambda calories:  calories > 1000, meals)

high_cal = filter( lambda meals:  meals.calories > 1000)

2 Answers

akak
akak
29,446 Points

Hey,

You are very close. Last one almost there :) Try this:

high_cal = filter(lambda meal: meal['calories'] > 1000, meals)

The first two "meal" could be "x" or in fact almost anything. It's just a name for variable, but you need to point to calories key and then as a second argument for filter put the dict.

James N
James N
17,864 Points

hey alak, thanks for the help! i was stuck on this exact codechallenge!

Thanks akak,

I knew it was something simple, but I would have spent hours more researching

before thinking to try some single quotes and some brackets.

I guess:

meal['calories']

..refers to the element of the meals' tuple whose value

is associated with the variable 'calories'.


After a bit more research I found this Functional Programming "docs" howto tutorial:

https://docs.python.org/3/howto/functional.html

where there is an example:

import functools
total = functools.reduce(lambda a, b: (0, a[1] + b[1]), items)[1]

Which the docs author says (referring to the example lines of code above):

If you try to do too much in a lambda statement, you’ll end up with an overly complicated expression that’s hard to read


Maybe this stackoverflow thread has the final word about python lambdas:

http://stackoverflow.com/questions/890128/why-python-lambdas-are-useful

...where someone named "javier" in the first answer says:

lambda is one of the requisites for a readable functional language. it's a pity that Python's syntax makes them so limited. still, it's miles ahead of any language without it.