runtime
163 / 500
question 163 Nested Data Structures · easy

Allergen counts across a range

A shop wants to know how many of its products contain each allergen. Open Food Facts lists allergens per product as tags like "en:gluten", and leaves the list out when it has none.

Functionallergen_counts(products)
productsdictionaries that may have "allergens_tags": a list of texts like "en:milk"
Returns{allergen: number of products} with the "en:" prefix removed, in the order allergens are first met
Rulesa product without allergens_tags contributes nothing; a tag without the en: prefix is kept as it is
Examples
callallergen_counts([{'code': '5010029000023', 'brands': ['Weetabix'], 'nutriments': {'energy-kcal_100g': 362, 'sugars_100g': 4.4, 'fiber_100g': 10}, 'allergens_tags': ['en:gluten']}, {'code': '7394376616228', 'brands': ['Oatly'], 'nutriments': {'sugars_100g': 4}, 'allergens_tags': ['en:gluten', 'en:oats']}, {'code': '0070470003023', 'brands': ['Yoplait'], 'nutriments': {'sugars_100g': 11.5}, 'allergens_tags': ['en:milk']}, {'code': '0028400090896', 'brands': ["Lay's"], 'nutriments': {}}])returns{'gluten': 2, 'oats': 1, 'milk': 1}
callallergen_counts([{'code': '0028400090896', 'brands': ["Lay's"], 'nutriments': {}}])returns{}
callallergen_counts([{'code': '0070470003023', 'brands': ['Yoplait'], 'nutriments': {'sugars_100g': 11.5}, 'allergens_tags': ['en:milk']}, {'code': '0070470003023', 'brands': ['Yoplait'], 'nutriments': {'sugars_100g': 11.5}, 'allergens_tags': ['en:milk']}])returns{'milk': 2}

Loading your progress…

Allergen counts across a range — Python practice · Runtime