title: Keeping track of index of parallelized for loop in Joblib
body: <p>In Python, I have a list of objects over which I need to iterate in a loop, and output a result for each iteration, <em>while also</em> keeping track of the index of the object being iterated over.</p>
  
  <p>Normally, this isn't a problem, since I can just use <code>enumerate</code> and do</p>
  
  <pre><code>results = []
  
  for index, value in enumerate(list_of_objects):
      ... *calculations* ...
  
      results.append([index, result_of_calculations])
  </code></pre>
  
  <p>However, lately my computations were taking too long, and so I started using <a href="https://joblib.readthedocs.io/en/latest/parallel.html" rel="nofollow noreferrer"><code>joblib</code></a> to parallelize my loops. However, now I cannot keep track of the index of operation with <code>enumerate</code>, because each piece of the loop can begin, and end, at irregular times, and I am stumped.</p>
  
  <p>How could I get code like the following to work, where each first value of the sub-array refers to the index of the object that was used for that particular iteration?</p>
  
  <pre><code>from joblib import Parallel, delayed
  
  def single_loop_function(x):
      single_output = *some calculations based on x*
      return single_output
  
  all_output = Parallel(n_jobs=-1, verbose=3, backend="loky")(
      map(delayed(single_loop_function), list_of_objects))
  
  print(all_output)
  [[0, *result*], [1, *result*], ... [5, *result*], [3, *result*]] 
  </code></pre>