nested for loop

Wolfgang Buechel wjb131 at web.de
Mon May 10 17:30:46 EDT 2004


Hi,

I want to iterate over all 2x2 matrices with elements in range 0..25 
(crypto-stuff). 

To produce them, first I wrote a fourfold nested for loop:

M=26
   for a in range(M):
      for b in range (M):
         for c in range (M):
            for d in range (M):
                 matr = [[a,b],[c,d]]
			(dosomething)

Then I had a look in comp.lang.python and found:


for (a,b,c,d) in [(x,y,z,t)  for x in range(M)
                             for y in range(M)
                             for z in range(M)
                             for t in range(M)] :
   matr = [[a,b],[c,d]]

Is there a shorter (and probably, with respect to exec time, faster) 
way to write such a 4for loop?
(I want to scan 3x3, 4x4 matrices too (;-)

-- Wolfgang



More information about the Python-list mailing list