I\'m trying to write code that finds the longest run in a list of Boolean values and return the index of the first and last value of that run. For example, if L is [False, Fals
Use itertools.groupby:
from itertools import groupby
values = [False, False, True, False, False, False, False, True, True, False, False]
start = 0
runs = []
for key, run in groupby(values):
length = sum(1 for _ in run)
runs.append((start, start + length - 1))
start += length
result = max(runs, key=lambda x: x[1] - x[0])
print(result)
Output
(3, 6)