I have a list of numbers, say
for i in list:
if x=y and i!=x
Here is a code that does exactly what you asked.
test=[0,1,0,1,0] # don't use 'list' as variable name for j in range(len(test)): # j=index of current i = (j-1) % len(test) # i=index of previous k = (j+1) % len(test) # k = index of next if test[i] == test[j] == test[j]: # equal to previous and next test[j] = test[i] # change to previous print(test)
The problem is that this code does nothing. If current value is equal to the preceding one (quote "whether its equal to its previous and the element after it"), the change will have no effect. For example, the condition will only be
True for three consecutive equal values
...7,7,7,...; so changing the current value to the previous will have no effect.
If you change your requirement to "whether it is equal to the preceding value or the one after it", change to
if (test[i] == test[j]) or (test[j] == test[k]):. (The first condition still is unnecessary).