if statement nested in for loop python