arr2 = [2, 3, 3, 1, 2, 2, 9, 4, 4]
quantity = []
for i in range(len(arr2)-1):
if arr2[i] == arr2[i+1]:
quantity[i] += 1
else:
quantity.append(1)
print(quantity)
I want to check if the current value is the same as the next value within the arr2
list. If it's not the same then add the number 1 to the quantity list, if it's the same then add 1 to the quantity and skip the next value.
Expected outcome:
quantity = [1,2,1,2,1,2]
Actual:
quantity[i] += 1
IndexError: list index out of range