nested if inside for loop in python