Learn Lambda Functions by Building an Expense Tracker - Step 21

Tell us what’s happening:

I’ve alredy tried to put the answer, that other people used to complete this step and its wont work.

Your code so far

def add_expense(expenses, amount, category):
    expenses.append({'amount': amount, 'category': category})
    
def print_expenses(expenses):
    for expense in expenses:
        print(f'Amount: {expense["amount"]}, Category: {expense["category"]}')
    
def total_expenses(expenses):
    return sum(map(lambda expense: expense['amount'], expenses))

def filter_expenses_by_category(expenses, category):
    return filter(lambda expense: expense['category'] == category)

expenses = []

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Learn Lambda Functions by Building an Expense Tracker - Step 21

What do the instructions say?

replace pass with a lambda function. Use expense as the parameter and return the comparison between the value of the 'category' key of the expense dictionary and category using the equality operator.

You’ve done this, looks good :white_check_mark:

You’ve also added something that the instructions did not ask for. :x:

The filter() function takes 2 arguments, you can read about it here: https://www.geeksforgeeks.org/filter-in-python/

Can you please show the code that you’ve written on your own? It’s kind of pointless to help you troubleshoot code that you’ve copied from other people.

I know, i tried without this function… but still doesnt work.

I did what the instructions say but it still doesn’t work

Please post your actual code instead of screenshots. Thanks

def filter_expenses_by_category(expenses, category):
return (lambda expense: expense[‘category’] == category, expenses)

That code isn’t properly intended so it will cause a syntax error

Here is a breakdown of how your code fulfills each part of the instructions:

#replace `pass` with a `lambda` function.
lambda 

#Use `expense` as the parameter
expense: 

# return the comparison between the value of the `'category'` 
# key of the `expense` dictionary and `category` using the 
# equality operator.
expense[‘category’] == category

That’s all you need to return, you can remove anything extra. I hope this helps!

thank you very much!

1 Like