You could make something a little bit more generic depending on what your pattern looks like. Instead of using an index based condition, you could apply a mask. e.g.
mask = [1, 1, 0, 0]
To create the mask where the values repeat such that it is the same length of your data, you can zip
the data alongside itertools.cycle(m)
. Example:
import itertools
def filter_mask(data, mask):
return [d for d, m in zip(data, itertools.cycle(mask)) if m]
Or use compress as @KellyBundy suggests!
def filter_mask(data, mask):
return list(itertools.compress(data, itertools.cycle(mask)))
Example
>>> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> filter_mask(data, mask=[1, 0])
[0, 2, 4, 6, 8, 10]
>>> filter_mask(data, mask=[0, 1])
[1, 3, 5, 7, 9]
>>> filter_mask(data, mask=[1, 1, 0, 0])
[0, 1, 4, 5, 8, 9]
>>> filter_mask(data, mask=[1, 0, 1, 1, 0])
[0, 2, 3, 5, 7, 8, 10]