nested if statement in for loop python