title: Python: Compute all possible pairwise distances of a list (DTW)
body: <p>I have a list of items like so: <code>T=[T_0, T_1, ..., T_N]</code> where each of <code>T_i</code> is itself a time series. I want to find the pairwise distances (via DTW) for all potential pairs. </p>
  
  <p>E.g. If <code>T=[T_0, T_1, T_2]</code> and I had a DTW function <code>f</code>, I want to find <code>f(T_0, T_1), f(T_0, T_2), f(T_1, T_2)</code>.</p>
  
  <p>Note <code>T_i</code> actually looks like <code>( id of i, [ time series values ] )</code>.</p>
  
  <p>My code snippet looks like this:</p>
  
  <pre><code>cluster = defaultdict( list )                                                                                                                                                                                                                                                                                                                           
  donotcluster = defaultdict( list )                                                                                                                          
  for i, lst1 in tqdm(enumerate(T)):                                                                                                           
      for lst2 in tqdm(T):                                                                                                                     
          if lst2 in cluster[lst1[0]] or lst2 in donotcluster[lst1[0]]:                                                                                       
              pass                                                                                                                                            
          else:                                                                                                                                               
              distance, path = fastdtw(lst1[1], lst2[1], dist=euclidean)                                                                                      
              if distance &lt;= distance_threshold:                                                                                                              
                  cluster[lst1[0]] += [ lst2 ]                                                                                                                
                  cluster[lst2[0]] += [ lst1 ]                                                                                                                
              else:                                                                                                                                           
                  donotcluster[lst1[0]] += [ lst2 ]                                                                                                           
                  donotcluster[lst2[0]] += [ lst1 ]
  </code></pre>
  
  <p>Right now I have around 20,000 time series and this take way too long (it will run in about 5 days). I am using the python library <code>fastdtw</code>. Is there a more optimised library? Or just a better/faster way of computing all possible distances? Since distances are symmetric I don't have to calculate for example <code>f(T_41,T_33)</code> if I have already calculated <code>f(T_33, T_41)</code></p>