Instead of
for a in range(1,27) for b in range(1,27) if a == 2*b ...
you could try for b in range(1,27) for a in [2*b] if a < 27 ...