I\'m looking for a way to easily determine if all not None items in a list occur in a single continuous slice. I\'ll use integers as examples of not None items
Here's a solution inspired by numpy. Get the array indices of all the non-null elements. Then, compare each index to the one following it. If the difference is greater than one, there are nulls in between the non-nulls. If there are no indices where the following index is more than one greater, then there are no gaps.
def is_continuous(seq):
non_null_indices = [i for i, obj in enumerate(seq) if obj is not None]
for i, index in enumerate(non_null_indices[:-1]):
if non_null_indices[i+1] - index > 1:
return False
return True