I'm trying to +=1 to an item in a dictionary if it exists, else create the key in the dictionary and set it to 1. I know I can use more than one line of logic for this, but I really want to see if its possible to keep the logic on one line. This is what I'm trying to do:
myDict = {}
myDict[100] += 1 if 100 in myDict else 1
It throw KeyError though if the key 100 doesn't exist. How can I set the one lined else logic to set key 100 to 1? Is it even possible?
Many thanks