-1

I have a list of brands:

brands = ['Nike', 'Reebok']

and a list of products:

products = ['Nike Air Max', 'Sony PS5', 'Samsung Galaxy S20', 'Apple Iphone 12', 'Air Jordan']

I need to check if any brand from the brands list was found in products list and then return new list of brands that were found.

I used a code from here.

brands_found = list(filter(lambda item: any(x in item for x in brands), products))

However it returns the items from products list. How can I change the code for it to return brands from brands list?

jabbor12
  • 3
  • 1

2 Answers2

2

Just flip around which list you're filtering and which list you search with any().

brands_found = [brand for brand in brands if any(brand in product for product in products)]

or if you want to use filter

brands_found = list(filter, lambda brand: any(brand in product for product in products))
Barmar
  • 741,623
  • 53
  • 500
  • 612
0
brands_found = [item for item in brands if any(item in i for i in products)]
  • 2
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Dec 04 '20 at 20:04