I think you can find the following useful, enlightening, and perhaps even surprising:
Edit: Moved the target value to the middle of the matrix to simulate an average location if the data is random and aligns the playing field for algorithms that stop as soon as they are found.
Timings were also performed for both Python 2 and comparison.
from __future__ import print_function import sys import timeit setup = """ mymatrix=[[1,2,3],[4,9,6],[7,8,5]] # moved target value to middle val = 9 """ statements = { "Anuk (OP)": """ # finds all occurrences found = [] for i,j in enumerate(mymatrix): for k,l in enumerate(j): if l==val: found.append((i,k)) """, "Ryan Haining": """ # only finds first occurrence in each row found = [(index, row.index(val)) for index, row in enumerate(mymatrix) if val in row] """, "martineau": """ # finds all occurrences width = len(mymatrix[0]) found = [] posn = 0 for row in mymatrix: for col in row: if col == val: found.append((posn // width, posn % width)) posn += 1 """, "martineau #2": """ # finds all occurrences width = len(mymatrix[0]) found = [(posn // width, posn % width) for posn,elem in enumerate(col for row in mymatrix for col in row) if elem == val] """, "mtahmed": """ # stops after it finds first occurrence matrix_dim = len(mymatrix[0]) item_index = 0 for row in mymatrix: for i in row: if i == val: break item_index += 1 if i == val: break found = [(int(item_index / matrix_dim), item_index % matrix_dim)] """, } N = 1000000 R = 3 timings = [ (idea, min(timeit.repeat(statements[idea], setup=setup, repeat=R, number=N)), ) for idea in statements] longest = max(len(t[0]) for t in timings) # length of longest name print('fastest to slowest timings (Python {}.{}.{})\n'.format(*sys.version_info[:3]), ' ({:,d} executions, best of {:d})\n'.format(N, R)) ranked = sorted(timings, key=lambda t: t[1]) # sort by speed (fastest first) for timing in ranked: print("{:>{width}} : {:.6f} secs, rel speed {rel:>8.6f}x".format( timing[0], timing[1], rel=timing[1]/ranked[0][1], width=longest))
Output Example:
fastest to slowest timings (Python 2.7.5) (1,000,000 executions, best of 3) mtahmed : 2.850508 secs, rel speed 1.000000x martineau : 3.684153 secs, rel speed 1.292455x Ryan Haining : 8.391357 secs, rel speed 2.943811x Anuk (OP) : 14.014551 secs, rel speed 4.916510x martineau #2 : 15.880949 secs, rel speed 5.571270x fastest to slowest timings (Python 3.3.2) (1,000,000 executions, best of 3) mtahmed : 5.019435 secs, rel speed 1.000000x martineau : 5.217747 secs, rel speed 1.039509x Ryan Haining : 5.705710 secs, rel speed 1.136723x Anuk (OP) : 8.317911 secs, rel speed 1.657141x martineau #2 : 11.590270 secs, rel speed 2.309078x