title: Defaultdict() the correct choice?
body: <p>EDIT: mistake fixed</p>
  
  <p>The idea is to read text from a file, clean it, and pair <em>consecutive</em> words (not permuations):</p>
  
  <pre><code>file = f.read()
  words = [word.strip(string.punctuation).lower() for word in file.split()]
  pairs = [(words[i]+" " + words[i+1]).split() for i in range(len(words)-1)]
  </code></pre>
  
  <p>Then, for each pair, create a list of all the possible <em>individual</em> words that can follow that pair throughout the text.  The dict will look like  </p>
  
  <pre><code>[ConsecWordPair]:[listOfFollowers]
  </code></pre>
  
  <p>Thus, referencing the dictionary for a given pair will return all of the words that can follow that pair.  E.g.</p>
  
  <pre><code>wordsThatFollow[('she', 'was')]
  &gt;&gt; ['alone', 'happy', 'not']
  </code></pre>
  
  <p>My algorithm to achieve this involves a <strong>defaultdict(list)</strong>...</p>
  
  <pre><code>wordsThatFollow = defaultdict(list) 
  
  for i in range(len(words)-1):
      try:
          # pairs overlap, want second word of next pair
          # wordsThatFollow[tuple(pairs[i])] = pairs[i+1][1]
          EDIT: wordsThatFollow[tuple(pairs[i])].update(pairs[i+1][1][0]
      except Exception:
          pass
  </code></pre>
  
  <p>I'm not so worried about the value error I have to circumvent with the 'try-except' (unless I should be).  The problem is that the algorithm <strong>only successfully returns one of the followers</strong>:</p>
  
  <pre><code>wordsThatFollow[('she', 'was')]
  &gt;&gt; ['not']
  </code></pre>
  
  <p>Sorry if this post is bad for the community I'm figuring things out as I go ^^</p>