title: Efficient Python search algorithm to find matches over moving time intervals
body: <p>I have a list of dictionaries like below:</p>
  
  <pre><code>listofdicts = [{'Time':2015-03-14 11:54:00, 'Value':'Some Value'},
                 {'Time':2015-03-14 13:23:00, 'Value':'Another Value'},
                 {'Time':2015-03-14 12:52:00, 'Value':'Some Value'}, ...]
  </code></pre>
  
  <p>I would like to search for dictionaries with following criteria within the list:
  Look for three or more dictionaries that have the same value for Value, with Time values within 10 minutes of each other. I would like this algorithm to create a new key within each dictionary that matches this criteria and mark it as matching.</p>
  
  <pre><code>e.g. The search algorithm would find:
  {'Time':2015-03-14 11:54:00, 'Value':'Same Value'}
  {'Time':2015-03-14 11:56:00, 'Value':'Same Value'}
  {'Time':2015-03-14 11:52:00, 'Value':'Same Value'}
  and add the matching key to each dictionary:
  {'Time':2015-03-14 11:54:00, 'Value':'Same Value', 'Matching':'True'}
  {'Time':2015-03-14 11:56:00, 'Value':'Same Value', 'Matching':'True'}
  {'Time':2015-03-14 11:52:00, 'Value':'Same Value', 'Matching':'True'}
  </code></pre>
  
  <p>I have created an algorithm to do this already, but it is not particularly efficient or scalable. Would anybody be able to offer any advice as to how to make it better or areas of research to look into?</p>
  
  <p>Current algorithm:</p>
  
  <pre><code>for dict in listofdicts:
     matchingdicts = []
     for dict2 in listofdicts:
        if dict['Value']==dict2['Value']:
           matchingdicts.append(dict2)
     listoftimeintervals = 
        [[dict['Time'] - datetime.timedelta(minutes=10),dict['Time']],
         [dict['Time'] - datetime.timedelta(minutes=9),dict['Time'] + datetime.timedelta(minutes=1)],
         ...,
         [dict['Time'],dict['Time'] + datetime.timedelta(minutes=10)]]
     for time in listoftimeintervals:
        dictsintimerange = []
        for matchingdict in matchingdicts:
           if time[0]&lt;=matchingdict['Time']&lt;=time[1]:
              dictsintimerange.append(matchingdict)
        if len(dictsintimerange)&gt;=3:
           for eachdict in dictsintimerange:
              eachdict['Matching']=='True'
  </code></pre>