Skip to main content
1 of 2
Mark Ransom
  • 1.9k
  • 2
  • 12
  • 15

There's no need to keep counts at all. All you need to do is keep track of whether the sequence is balanced or not as you check every element. And the special tests you had for an empty list or odd list length are redundant.

def balanced(lst):
    tilt = 0
    for item in lst:
        if item % 2 == 0:      #even
            tilt += 1
        else:                  #odd
            tilt -= 1
    return tilt == 0
Mark Ransom
  • 1.9k
  • 2
  • 12
  • 15