[Solved] Continuously adding to a dictionary [closed]


Right now, the else clause fires if there is a at least one key that does not match vehicle. Fix it like this:

def add_to_dict(product_type, brand, product_dict):
    if product_type in product_dict:
        product_dict[product_type].append(brand)
    else:
        product_dict[product_type] = [brand]

product_dict = {}
add_to_dict("car", "audi", product_dict)
add_to_dict("car", "mercedes", product_dict)
add_to_dict("phone", "apple", product_dict)

print(product_dict)
# Output:
# {"car": ["audi","mercedes"], "phone": ["apple"]}

solved Continuously adding to a dictionary [closed]