First, .append()
with 2 arguments isn't going to work. You probably meant to append a tuple or list consisting of ri
and ei
.
Also, please don't use built–in names like list
for variables – that's confusing for everyone, including the interpreter.
And as for solving your problem in an efficient manner, it would be best to iterate over list
(here values_sought
) not in the outer–, but in the innermost loop, so as to avoid pointless checking of the same matrix coordinates multiple times, like this:
values_sought = [1, 2, 3]
matrix = [[0,1,2], [3,2,0], [1,2,3]]
coordinates = []
for row_index, row in enumerate(matrix):
if not values_sought: break
for column_index, value_present in enumerate(row):
if not values_sought: break
for value_sought_index, value_sought in enumerate(values_sought):
if value_present == value_sought:
coordinates.append((row_index, column_index))
values_sought.pop(value_sought_index)
break
Values are removed from values_sought
after being found, so you may want to have a temporary list for that purpose if you need to still have these values afterwards.