Suppose I have dictionary and I want to fill that with some keys and values , first dictionary is empty and suppose I need this dictionary for a counter for example count some keys in a string I have this way:
myDic = {}
try :
myDic[desiredKey] += 1
except KeyError:
myDic[desiredKey] = 1
Or maybe some times values should be a list and I need to append some values to a list of values I have this way:
myDic = {}
try:
myDic[desiredKey].append(desiredValue)
except KeyError:
myDic[desiredKey] = []
myDic[desiredKey].append(desiredValue)
Is there better alternarive for this works (both of them) that dont uses try except
section?