How do I parallelize multiplication of 2 elements in a nested list. ie if my list is:
lst = [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
The output I want to get is:
[1, 2, 3, 2, 4, 6, 3, 6, 9]
where 1*1=1, 1*2=2, 1*3=3, 2*1=2 and so on.
I have the following code:
from itertools import product
from joblib import Parallel, delayed
lst = (list(a) for a in product([1, 2, 3], repeat=2))
results = Parallel(n_jobs=-1)(delayed(x*y for x, y in lst))
results
But this code is giving me an error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-60-68b5134d22bd> in <module>
----> 1 results = Parallel(n_jobs=-1)(delayed(x*y for x, y in lst))
2 results
~\Anaconda3\lib\site-packages\joblib\parallel.py in __call__(self, iterable)
964 if hasattr(self._backend, 'start_call'):
965 self._backend.start_call()
--> 966 iterator = iter(iterable)
967 pre_dispatch = self.pre_dispatch
968
TypeError: 'function' object is not iterable
1) Where have I gone wrong?
2) How do I fix this?