I have a list of index's as such:
index_max = [5, 5, 5, 5, 6, 7, 14, 15, 16, 16, 16, 16, 18, 18, 32, 32, 34, 34, 34, 34, 35, 38, 42, 42, 42, 42, 45]
and I am given a:
pattern_width = 4
n = pattern_width
final_list = []
How would I run through the list analysing n elements at a time, in which if there is a case of all the elements are of equal values , they are appended to an empty list?
So, here as the first 4 elements are [5, 5, 5, 5], the value 5 would be appended to final_list. However, as the next 4 elements are [5, 5, 5, 6], that would not be appended.
The solutions would be [5, 16, 34, 42]
The problem I keep running into is that list index out of range
.
My approach was:
for i in range(len(index_max)):
x = index_max[i]==index_max[i+(pattern_width)
final_list.append(x)
However, this is unable to work at the end of the list. How would I fix this? Thank you.