Learn Lambda Functions by Building an Expense Tracker - Step 30

hello this is my first post.
on this func :
def filter_expenses_by_category(expenses, category):
filter(lambda expense, expenses: expense[‘category’] == category)

i was asked to add expenses as the second argument, filter the whole thing.
i did that still no success.
my question is why?

thank you in advance

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))
    

# User Editable Region

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

# User Editable Region


expenses = []

Your browser information:

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

Challenge Information:

Learn Lambda Functions by Building an Expense Tracker - Step 30

use the lambda declaration from previous challenge and place the list in the last
filter(lambda, list)

As @ghulamshabirbaloch mentioned, the filter() takes two arguments, which are separated by a comma. Look at the example syntax again:

filter(my_function, my_list)

In your code the comma is in the middle of the lamba function. Don’t change the lambda function.

expenses list as the second argument.

expenses should be the second argument of the filter() function.

Thank you

Filter(lambda argument1, argument2 : x1==x2)
Is this correct?
Im really confused sorry :disappointed:

You don’t need to write “argument”. It’s the same as passing two arguments to the print() function:

print("This is the first argument", "and this is the second")

Two arguments separated by a comma.

Your code starts like this:

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

The first argument will be the entire lambda function: lambda expense: expense['category'] == category

Then a comma to separate your arguments.

The second argument will be the expenses list defined in the parameters.

Thanks mate!

ill try again later on when i get home. I know i didn’t have to write argument it just an example… .

The instruction said i have to pass two arguments… the second being expenses. I looked up and find out that the arguments are before the : .

That’s why i got confused .

That’s not correct, the colon is part of the lambda function.

The arguments are anything passed to the filter function between the brackets. If there’s more than one, separate by a comma.

function(argument)
function(argument, argument2)
print("something")
print("something", "else")

The entire lambda function should be passed as the first argument, followed by a comma.

hello and thank you again for your explanation.

code removed

that’s what they wated

1 Like

Glad you got it! Please don’t share the solution code.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Welcome to the forum, please enjoy your stay!