title: Count word frequency efficient in Python using dictionary
body: <p>My task is to find the frequency of each word in a list. There are two ways to it. </p>
  
  <p><strong>Method1</strong></p>
  
  <pre><code>def f(words):
      wdict = {}
      for word in words:
          if word not in wdict:
              wdict[word] = 0
          wdict[word] += 1
      return wdict
  </code></pre>
  
  <p><strong>Method2</strong></p>
  
  <pre><code>def g(words):
      wdict = {}
      for word in words:
          try:
              wdict[word] += 1
          except KeyError:
              wdict[word] = 1
  </code></pre>
  
  <p>Why is Method 2 efficient? Isn't in both the cases, the number of hash functions calls is same in contradiction to this <a href="http://blackecho.github.io/blog/programming/2016/03/23/python-underlying-data-structures.html" rel="nofollow noreferrer">http://blackecho.github.io/blog/programming/2016/03/23/python-underlying-data-structures.html</a>?</p>