In [1]:
import urllib.request
from collections import defaultdict, Counter

wp_url = "https://en.wikipedia.org/wiki/Budapest"
text = urllib.request.urlopen(wp_url).read().decode('utf8')
words = text.split()

In [2]:
%%timeit
word_freq = {}
for word in words:
    if word not in word_freq:
        word_freq[word] = 1
    else:
        word_freq[word] += 1


10.3 ms ± 670 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [3]:
%%timeit
word_freq = {}
for word in words:
    try:
        word_freq[word] += 1
    except KeyError:
        word_freq[word] = 1


15.1 ms ± 720 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [4]:
%%timeit
word_freq = {}
for word in words:
    word_freq.setdefault(word, 0)
    word_freq[word] += 1


17.2 ms ± 899 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [5]:
%%timeit
word_freq = defaultdict(int)
for word in words:
    word_freq[word] += 1


12.4 ms ± 520 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [6]:
%%timeit
word_freq = Counter(words)


7.25 ms ± 322 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)