How to keep every nth item in a list and make the rest zeros
<p>I am trying to model and fit to noisy data over a long time series and I want to see what happens to my fit if I remove a substantial amount of my data.</p>

<p>I have a long time-series of data and I am only interested in every nth item. However I still want to plot this list over time but with every other unwanted element removed.</p>

<p>For example, for n=4, the list</p>

<pre><code>a = [1,2,3,4,5,6,7,8,9,10...]
</code></pre>

<p>Should become</p>

<pre><code>a_new = [1,0,0,0,5,0,0,0,9,0...]
</code></pre>

<p>I don't mind if the <em>position</em> of the nth item is at the start or end of the sequence, my series is effectively arbitrary and so long that it won't matter what I delete. For example 'a_new' could also be:</p>

<pre><code>a_new = [0,0,0,4,0,0,0,8,0,0...]
</code></pre>

<p>Ideally the solution wouldn't depend on the length of the list, but I can have that length as a variable.</p>

<p>Edit 1:</p>

<p>I actually wanted empty elements, not zero's, (if that's possible?) so:</p>

<pre><code>a_new = [1,,,,5,,,,9...] 
</code></pre>

<p>Edit 2:</p>

<p>I needed to remove the corresponding elements from my time series too so that when everything is plotted, each data element has the same index as the time series element.</p>

<p>Thanks!</p>

<p>Here's a working example to filter functions given a certain step K:</p>

<pre><code>def filter_f(data, K=4):
    if K &lt;= 0:
        return data

    N = len(data)
    f_filter = [0 if i % K else 1 for i in range(N)]
    return [a * b for a, b in zip(data, f_filter)]

f_input = range(10)

for K in range(10):
    print("Original function: {0}".format(f_input))
    print("Filtered function (step={0}): {1}".format(
        K, filter_f(f_input, K)))
    print("-" * 80)
</code></pre>

<p>Output:</p>

<pre><code>Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=0): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=1): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=2): [0, 0, 2, 0, 4, 0, 6, 0, 8, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=3): [0, 0, 0, 3, 0, 0, 6, 0, 0, 9]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=4): [0, 0, 0, 0, 4, 0, 0, 0, 8, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=5): [0, 0, 0, 0, 0, 5, 0, 0, 0, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=6): [0, 0, 0, 0, 0, 0, 6, 0, 0, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=7): [0, 0, 0, 0, 0, 0, 0, 7, 0, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=8): [0, 0, 0, 0, 0, 0, 0, 0, 8, 0]
--------------------------------------------------------------------------------
Original function: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Filtered function (step=9): [0, 0, 0, 0, 0, 0, 0, 0, 0, 9]
--------------------------------------------------------------------------------
</code></pre>

python list