python Improve a function in a elegant way I have a grid as 

&gt;&gt;&gt; data = np.zeros((3, 5))
&gt;&gt;&gt; data
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]]


i wrote a function in order to get the ID of each tile.

array([[(0,0), (0,1), (0,2), (0,3), (0,4)],
      [(1,0), (1,1), (1,2), (1,3), (1,4)],
      [(2,0), (2,1), (2,2), (2,3), (2,4)]]


def get_IDgrid(nx,ny):
    lstx = list()
    lsty = list()
    lstgrid = list()
    for p in xrange(ny):
        lstx.append([p]*nx)
    for p in xrange(ny):
        lsty.append(range(0,nx))
    for p in xrange(ny):
        lstgrid.extend(zip(lstx[p],lsty[p]))
    return lstgrid


where nx is the number of columns and ny the number of rows

test = get_IDgrid(5,3)
print test
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]


this function will be embed inside a class

class Grid(object):
    __slots__= ("xMin","yMax","nx","ny","xDist","yDist","ncell")
    def __init__(self,xMin,yMax,nx,ny,xDist,yDist):
        self.xMin = xMin
        self.yMax = yMax
        self.nx = nx
        self.ny = ny
        self.xDist = xDist
        self.yDist= yDist
        self.ncell = nx*ny
Numpy has built in functions for most simple tasks like this one. In your case, numpy.ndindex should do the trick:

&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; [j for j in np.ndindex(3, 5)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4),
 (2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]


You can get the same result in a similarly compact way using itertools.product :

&gt;&gt;&gt; import itertools
&gt;&gt;&gt; [j for j in itertools.product(xrange(3), xrange(5))]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4),
 (2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]


EDIT Note that the (now corrected) order of the parameters is reversed with respect to the OP's get_IDgrid.

Both expressions above require a list comprehension, because what gets returned is a generator. You may want to consider whether you really need the whole list of index pairs, or if you could consume them one by one.