After importing a bunch of objects into a Maya scene, all at origin, I'd like to organize them in a grid-like arrangement with every cell of the grid filled. I came up with a solution, but it doesn't really work as more rows and columns get created.
It leaves empty spots like in this illustration:

Any idea on how to fix the arrangement?
The code I currently have:
import maya.cmds as mc
#for iteration loops
offsetAmtZ= 40
offsetAmtX= 40
switchBool= True
diagonalMove= True
#select all ctrls
mc.select(all=True)
allCtrls= mc.ls(sl=True)
for each in allCtrls:
if diagonalMove== True:
mc.move(offsetAmtX,0,offsetAmtZ, each)
diagonalMove= False
else:
if switchBool== True:
mc.move(0,0,offsetAmtZ, each)
switchBool= False
offsetAmtZ= offsetAmtZ + 40
else:
mc.move(offsetAmtX,0,0, each)
switchBool= True
offsetAmtX= offsetAmtX + 40
diagonalMove= True