I am checking consecutive indices of a list and I want to execute same piece of code in case if the consecutive elements are not equal or if the list index is out of range. Here's what I'm trying
for n in range(len(myList))
try:
if myList[n]==myList[n+1]:
#some code
else:
#if they are not equal then do something
#same code should execute if exception raised: index error --> how do i do this?
Is there a way to do this elegantly without having to repeat the same code in the except block somehow?