You could define a list or a dictionary of indices that are not present and add an $$\texttt{if}$$-clause to your quicksum call.
import gurobipy as gpm = gp.Model()xI = 1xJ = 2x  = m.addVars(xI, xJ, name="x")yI = 3yJ = 4y  = m.addVars(yI, yJ, name="y")present = {}# loop over the bigger set yI x yJfor i in range(yI):  for j in range(yJ):    if i < xI and j < xJ:      present[i,j] = True    else:      present[i,j] = Falsem.setObjective(  gp.quicksum(x[i,j]*y[i,j]               for i in range(yI)              for j in range(yI)               if present[i,j]))
Note that the above snippet works only if $$x_I \times x_J$$ is a subset of $$y_I \times y_J$$ and might require more work if you are using other indices.