Chris Ged
Chris Ged

Reputation: 1

Lambda function with filter

Only include products that are non-discontinued (discontinued=0) with unit_price greater than 15.0 in the new list. How I can solve this? How can I select and use discontinued=0 with unit_price and put it into the code?

print (list(filter(lambda x: x=0,>15,products)))

Upvotes: 0

Views: 62

Answers (1)

Djaouad
Djaouad

Reputation: 22794

I imagine products are either objects of a certain Product class, or are dicts, if that's the case, you can use:

For objects of a certain class (having discontinued and unit_price attributes):

print(list(filter(lambda x: x.discontinued == 0 and x.unit_price > 15, products)))

For dicts (having discontinued and unit_price keys):

print(list(filter(lambda x: x['discontinued'] == 0 and x['unit_price'] > 15, products)))

Upvotes: 2

Related Questions