title: Python Nested For Loop Array Comparison - Possibility of Optimization?
body: <p>I'm trying to optimize a nested for loops that <strong>compares an element in the array with the rest of the elements in the array</strong>.</p>
  
  <p>There's two part, the first part is for example,  an Array has 3 elements, and each element is a dictionary:</p>
  
  <p><code>[{"someKey_1":"a"}, {"someKey_1":"b"}, {"somekey_1":"a"}]</code></p>
  
  <hr>
  
  <p>1st iteration(1st element compares with 2nd element):</p>
  
  <p>Test key of "someKey" for two elements, since a != b, then we do nothing</p>
  
  <hr>
  
  <p>2st iteration(1st element compares with 3nd element):</p>
  
  <p>Test key of "someKey" for two elements, since a == a, we do some logic</p>
  
  <hr>
  
  <p>The code:</p>
  
  <pre><code>for idx, val in enumerate(set_of_pk_values):
      for idx_2, val_2 in enumerate(set_of_pk_values):
          if (val['someKey'] == val_2['someKey'] and idx != idx_2):
                  #Some Logic
  </code></pre>
  
  <hr>
  
  <p>The second part is very similar to the previous example (3 items in the list), in the same dictionary, we have an array associated with a key (now there's a single dictionary with two keys in each element of the array), let's say:</p>
  
  <pre><code>[{"someKey_1":[b,f]}{"someKey_2":a}, 
  {"someKey_1":[e,f]}{"someKey_2":b}, 
  {"somekey_1":[h,k]}{"someKey_2":c}]
  </code></pre>
  
  <hr>
  
  <p>1st iteration (1st element compares with 2nd element):</p>
  
  <p>loops through the array with the key: someKey_1</p>
  
  <p><code>b==b</code> (2nd element's someKey_2), then do some logic</p>
  
  <p><code>f!=b</code> (2nd element's someKey_2), no logic is done</p>
  
  <hr>
  
  <p>2nd iteration (1st element compares with 3rd element):</p>
  
  <p>loops through the array with the key: someKey_1</p>
  
  <p><code>b==c</code> (3rd element's someKey_2), then do some logic</p>
  
  <p><code>f!=c</code> (3rd element's someKey_2), no logic is done</p>
  
  <hr>
  
  <p>The code:</p>
  
  <pre><code>for idx, val in enumerate(set_of_pk_values):
      for idx_2, val_2 in enumerate(set_of_pk_values):
          for pred in val['someKey_1']:
              if(val_2['someKey_2'] == pred):
                  #Some Logic
  </code></pre>
  
  <hr>
  
  <p>Currently the runtime for the first nested loop: 21 seconds, and the second nested loop is around 19 seconds. Compared to other processes, ranging from 1-2 seconds, this part is clearly a bottleneck.</p>
  
  <p>Can anybody point me to the right direction on how to optimize this piece of simple, yet extremely time consuming code?</p>