I'm trying to directly unpack the tuple returned from np.unravel_index()
in a for
loop definition, but I'm running into the following issue:
for i,j in np.unravel_index(range(len(10)), (2, 5)):
print(i,j)
returns:
ValueError: too many values to unpack (expected 2)
I can solve the issue by doing:
idx = np.unravel_index(range(len(10)), (2, 5))
for i, j in zip(idx[0], idx[1]):
print(i, j)
but I really feel I should be able to do it all in the for
loop assignment.
I have looked through StackOverflow and found nothing that could help me with my specific question.
Solution:
As the accepted solution I think that this does exactly what I want i.e., unpack directly in the for
loop assignment and without previous knowledge of the dimensions of idx
:
for i, j in zip(*np.unravel_index(range(len(10)), (2, 5)):
print(i, j)