I have written a more complex program on python, but I'm stuck on trying to find out if a list contains a given string.
Simplified problem:
product_names = ['string1', 'string2']
products = [{'id': 'string1', 'test': 'test1 - value'}, {'id': 'string3', 'test': 'test2 - value'}]
# prints: string1 string3
product_ids = (p['id'] for p in products)
for ids in product_ids:
print(ids)
# doesn't print found
for p in product_names:
if p in product_ids:
print('found')
# doesn't print missing product names
if not all(p in product_ids for p in product_names):
print('missing product names')
I don't get why this doesn't work, do I have to restart the starting index somehow, and is so, how?
product_idsis a generator; you can't useinon it. Use[p['id'] for p in products]instead of(p['id'] for p in products)to make it a list instead.product_ids = (p['id'] for p in products)-->product_ids = [p['id'] for p in products]