why don't you just count different adjacent values and add one finally. That's it.
count = 1
# Iterate through the list starting from the second element
for i in range(1, len(X)):
# If the current element is different from the previous one, it's alternating
if X[i] != X[i - 1]:
count += 1
return count