I am trying to improve my understanding about dictionary list comprehension. I have created a silly list, based on what is in my driveway:
car = ['Ram', 'Ford', "Jeep", 'Jeep', 'ram']
I can create a dictionary and loop through the list
cardict = dict()
for count in car:
count = count.upper()
cardict[count] = cardict.get(count, 0) + 1
print(cardict)
That returns
{'RAM': 3, 'DODGE': 1, 'FORD': 1}
I have also been trying to improve my understanding of comprehension. So, I have also tried using fromkeys() and get()
For example,
again = dict.fromkeys(cardict, 0) + 1
However, I am getting a Type Error. How can I go through the dictionary and increment items in a list? I am looking for the "Pythonic" way to do this, so I would assume that there is a way without creating a for loop. Is it possible?