I need to access the next element of an array to compare it with the previous one, try to do it by indexes however the index is out of range
lista = [1,2,2,3,4,5,5,6,7,8]
for i in range(len(lista)):
if lista[i]==lista[i+1]: print("same number")
You can simply check the size of the list before doing the process. check the code bellow
for i in range(len(lista)):
if i == len(lista)-1:
print("end Process")
else:
a = lista[i]
b = lista[i+1]
if a == b:
print( str(a) + " and " + str(b) + " are the same number ")
or using len(lista)-1 in your for loop
for i in range(len(lista)-1):
a = lista[i]
b = lista[i+1]
if a == b:
print( str(a) + " and " + str(b) + " are the same number ")
len(lista)-1since the last element has no nextiwill be assigned here? If it is out of range oflista, then perhaps change the loop so that it never has a value greater than the largest index.