It depends a lot on data type of i
.
If you're able to store i
as array, it's better to use np.transpose
instead of zip:
z = np.zeros([3, 3])
i = np.array([[1, 1], [2, 0], [0, 2]])
x, y = np.transpose(i)
z[x, y] = 1
It will change its view and that's costless. You need to unpack only two arguments x
and y
which is near costless too.
In comparison, zip
creates a generator for Python - style iteration of every item in i
which is more expensive:
z = np.zeros([3, 3])
i = [[1, 1], [2, 0], [0, 2]]
x, y = z[tuple(zip(*i))] = 1
z[x, y] = 1
The worst thing you can do is to call zip on numpy array because they are not designed for fast iteration.
There are some test that demonstrates advantages and disadvantages of zip
vs np.transpose
:
z = np.zeros([3000, 3000])
ii = np.random.randint(3000, size=(1500000, 2))
i = ii.tolist()
%%timeit
x, y = np.transpose(i) #lots of time spent for conversion into arr
z[x, y] = 1
3.07 s ± 32.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
x, y = np.transpose(ii)
z[x, y] = 1
106 ms ± 1.47 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%%timeit
z[tuple(zip(*i))] = 1
1.26 s ± 14.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)