We'll have a list: cars = ['nissan','nobel','nobel', 'porsche', 'merc', 'merc', 'porsche', 'aston martin']
One way to solve this problem is to iterate over the list. We'll initialize a dictionary, carCounts
, that keeps track of how many times each car appears
carCounts = defaultdict(int)
for car in cars:
carCounts[car] += 1
print carCounts