 # Count sequential number of occurrence of items in list

Hi there, I’m trying to run a counting operation on a true/false boolean list. Within a list of values, I want to count any continuous sequential occurrences of “false” BEFORE the 1st true, then also count any continuous sequential occurrences of “false” AFTER the last true. Is there a data structure that will do this? I tried starting with getting the index of the occurrences but I got stuck because it only lists the index of the first occurrence. So in this example I would get (1,0),(1,1),(1,1)… and so on

for something like this:

false
false
true
false
true
false
false

I should get: (2,2)

Thanks!

Answered my own question! Used the List.FirstIndexOf to get number BEFORE and then reversed the list to the get number AFTER!

Hope this helps others who are having the same issue!

Thanks

@dgudgenov Or you can do something like this:

``````from itertools import groupby
OUT = []
for i in IN:
gc,keys,temp = [],[],[]
for k,g in groupby(i):
keys.append(k)
if k is not True:
gc.append(len(list(g)))

if len(gc) == 1:
if keys == True:
temp.append([0,gc])
else:
temp.append([gc,0])
elif len(gc) > 1:
if keys[-1] == True:
temp.append([gc,0])
else:
temp.append(gc[::len(gc)-1])
else:
temp.append(gc)

OUT.append(*temp)
``````