python better way to set dictionary value [closed]

what is a better way to do this in python current = {}

    if key in current:
        current[key] += 1
    else:
        current[key] = 1

Thanks for any help

Answer

Use setdefault:

current = {}
a = ['a', 'b', 'a']
for i in a:
    current[i] = current.setdefault(i, 0) + 1
print(current)

Output:

{'a': 2, 'b': 1}

Or use get:

current = {}
a = ['a', 'b', 'a']
for i in a:
    current[i] = current.get(i, 0) + 1

Or if you want to get the frequency count:

from collections import Counter
a = ['a', 'b', 'a']
current = Counter(a)
print(current)

Output:

Counter({'a': 2, 'b': 1})