title: python IndexError: list index out of range
body: <p>I get a IndexError: list index out of range Error. I have get the recipient lists for each message. I have collapsed the lists of recipients into a single list. How do I fix that problem?</p>
  
  <pre><code>import json
  import pymongo # pip install pymongo
  from bson import json_util # Comes with pymongo
  import re
  from pymongo import MongoClient
  # The basis of our query
  FROM = "kenneth.lay@enron.com"
  
  
  client = pymongo.MongoClient('mongodb://user:user123@ds033499.mongolab.com:33499/enron')
  db = client.enron
  mbox = db.mbox
  
  # Get the recipient lists for each message
  
  recipients_per_message = db.mbox.aggregate([
      {"$match" : {"From" : re.compile(r".*{0}.*".format(FROM), re.IGNORECASE)}}, 
      {"$project" : {"From" : 1, "To" : 1} }, 
      {"$group" : {"_id" : "$From", "recipients" : {"$addToSet" : "$To" } } }                    
      ])['result'][0]['recipients']
  
  # Collapse the lists of recipients into a single list
  
  all_recipients = [recipient
                    for message in recipients_per_message
                    for recipient in message]
  
  # Calculate the number of recipients per sent message and sort
  
  recipients_per_message_totals = \
      sorted([len(recipients) 
      for recipients in recipients_per_message])
  
  # Demonstrate how to use $unwind followed by $group to collapse
  # the recipient lists into a single list (with no duplicates
  # per the $addToSet operator)
  
  unique_recipients = db.mbox.aggregate([
      {"$match" : {"From" : re.compile(r".*{0}.*".format(FROM), re.IGNORECASE)}}, 
      {"$project" : {"From" : 1, "To" : 1} }, 
      {"$unwind" : "$To"}, 
      {"$group" : {"_id" : "From", "recipients" : {"$addToSet" : "$To"}} }
      ]['result'][0]['recipients'])
  
  print all_recipients
  print "Num total recipients on all messages:", len(all_recipients)
  print "Num recipients for each message:", recipients_per_message_totals
  print "Num unique recipients", len(unique_recipients)
  </code></pre>
  
  <p>this is the traceback</p>
  
  <pre><code>  IndexError Traceback (most recent call last)
    &lt;ipython-input-85-b1e01d6382fb&gt; in &lt;module&gt;()
     18   {"$project" : {"From" : 1, "To" : 1} },
     19   {"$group" : {"_id" : "$From", "recipients" : {"$addToSet" : "$To" } } }
     ---&gt;20 ])['result'][0]['recipients']
     21 
     22 # Collapse the lists of recipients into a single list
  
     IndexError: list index out of range
  </code></pre>