Reputation: 11
I'm trying to create a dictionary that has multiple values per key. For example:
top_10 = ['Volkswagen_Golf_1.4', 'BMW_316i', 'Ford_Fiesta', 'BMW_318i', 'Volkswagen_Polo', 'BMW_320i', 'Opel_Corsa', 'Renault_Twingo', 'Volkswagen_Golf', 'Opel_Corsa_1.2_16V']
common_brands = ['volkswagen', 'bmw', 'opel', 'mercedes_benz', 'audi', 'ford']
I want to create a dictionary that looks like this:
{'volkswagen': ['Volkswagen_Golf_1.4', 'Volkswagen_Polo', 'Volkswagen_Golf'], 'bmw': ['BMW_316i', 'BMW_318i', 'BMW_320i'], 'opel': ['Opel_Corsa', 'Opel_Corsa_1.2_16V'],'ford': ['Ford_Fiesta'], 'Renault': ['Reanault_Twingo']}
With the code I've tried, I can only get one model per brand, and I can't find a way to add the brands that aren't in the common_brands list.
models_by_brand = {}
for brand in common_brands:
for model in top_10:
if brand in model.lower():
models_by_brand[brand] = [model]
models_by_brand
Output:
{'bmw': ['BMW_320i'],
'ford': ['Ford_Fiesta'],
'opel': ['Opel_Corsa_1.2_16V'],
'volkswagen': ['Volkswagen_Golf']}
Upvotes: 1
Views: 62
Reputation:
The traditional solution to your problem would be to check if the key exists in your dictionary. If it does not, you initialise it to an empty list, else you append to that list.
models_by_brand = {}
for brand in common_brands:
for model in top_10:
if brand in model.lower():
if brand not in models_by_brand:
models_by_brand[brand] = list()
models_by_brand[brand].append(model)
print(models_by_brand)
The other solution would be to use a defaultdict. Defaultdict is used to pre-initialise a default value for any key.
from collections import defaultdict
models_by_brand = defaultdict(list)
for brand in common_brands:
for model in top_10:
if brand in model.lower():
models_by_brand[brand].append(model)
print(models_by_brand)
Upvotes: 0
Reputation: 37616
You could use defaultdict
and split the name of the vehicles to get the brand (if these are normalized):
from collections import defaultdict
models_by_brand = defaultdict(list)
for model in top_10:
brand = model.lower().split('_')[0]
models_by_brand[brand].append(model)
By using defaultdict
, you can write models_by_brand[brand].append(model)
and if there are currently no models for brand
in the dictionary, an empty list will be created and used.
Upvotes: 2
Reputation: 121
# The following code should work just fine.
top_10 = ['Volkswagen_Golf_1.4', 'BMW_316i', 'Ford_Fiesta', 'BMW_318i', 'Volkswagen_Polo', 'BMW_320i', 'Opel_Corsa',
'Renault_Twingo', 'Volkswagen_Golf', 'Opel_Corsa_1.2_16V']
common_brands = ['volkswagen', 'bmw', 'opel', 'mercedes_benz', 'audi', 'ford']
result = {}
cars = []
# For each car brand
for k in common_brands:
# For each car model
for c in top_10:
# if car brand present in car model name append it to list
if k.lower() in c.lower():
cars.append(c)
# if cars list is not empty copy it to the dictionary with key k
if len(cars) > 0:
result[k] = cars.copy()
# Reset cars list for next iteration
cars.clear()
print(result)
Upvotes: 0
Reputation: 6132
If you want to keep the structure of your code, use a list:
models_by_brand = {}
for brand in common_brands:
model_list=[]
for model in top_10:
if brand in model.lower():
model_list.append(model)
models_by_brand[brand] = model_list
models_by_brand = {k:v for k,v in models_by_brand.items() if v!=[]}
Output:
{'volkswagen': ['Volkswagen_Golf_1.4', 'Volkswagen_Polo', 'Volkswagen_Golf'],
'bmw': ['BMW_316i', 'BMW_318i', 'BMW_320i'],
'opel': ['Opel_Corsa', 'Opel_Corsa_1.2_16V'],
'ford': ['Ford_Fiesta']}
@Holt's answer would be the most efficient answer, though.
Upvotes: 0