You should put that out of the loop and the len(pairs) == 0 check on top of the function.