I'm trying to find the range of positions that duplicates lie over in a list.
For instance in the list l = [1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 5, 6,7,7, 7] 2 is duplicated between positions 2 and 5. My code works until the very end where an error is caused by what I assume is the inner loop not keeping track of the array's length.
How should I go about in fixing this error?
Code:
l = [1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 5, 6,7,7, 7]
i = 0
out = []
while i < (len(l)):
ogPos = i
ogVal = l[i]
while (l[i] == ogVal):
print(i)
i += 1
edPos = i-1
print(f'\t{ogPos}\t{edPos}\t{ogVal}')
Output:
0
1
0 1 1
2
3
4
5
2 5 2
6
7
8
6 8 3
9
9 9 4
10
10 10 5
11
11 11 6
12
13
14
Traceback (most recent call last):
File "testlist.py", line 8, in <module>
while (l[i] == ogVal):
IndexError: list index out of range