## How to keep every nth item in a list and make the rest zeros

Tags: python list
Question!

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.

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.

For example, for n=4, the list

``````a = [1,2,3,4,5,6,7,8,9,10...]
``````

Should become

``````a_new = [1,0,0,0,5,0,0,0,9,0...]
``````

I don't mind if the position 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:

``````a_new = [0,0,0,4,0,0,0,8,0,0...]
``````

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

Edit 1:

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

``````a_new = [1,,,,5,,,,9...]
``````

Edit 2:

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.

Thanks!

``````[0 if i%4 else num for i, num in enumerate(a)]
``````def filter_f(data, K=4):
``` By : BPL ```
``` Use a list comprehension with a ternary conditional that takes the mod of each element on the number n: By : Moses Koledoye This video can help you solving your question :) By: admin ```
``` Related Questions Python: what is the difference between (1,2,3) and [1,2,3], and when should I use each? A Transpose/Unzip Function in Python (inverse of zip) Best way to check if a list is empty In Python, how can you easily retrieve sorted items from a dictionary? (adsbygoogle = window.adsbygoogle || []).push({}); How do I sort a list of dictionaries by values of the dictionary in Python? How do I split a string into a list? In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique *while preserving order*? [duplicate] Is there a zip-like method in .Net? List all words in a dictionary that start with <user input> How can I join a list into a string (caveat)? Iterate a list with indexes in Python Clearing a list Removing a subset of a dict from within a list Python List vs. Array - when to use? Finding the index of an item given a list containing it in Python fast filter method in python How do I reverse a list using recursion in Python? Passing a list while retaining the original Given a list of variable names in Python, how do I a create a dictionary with the variable names as keys (to the variables' values)? Python list of lists, changes reflected across sublists unexpectedly ```
``` ```
``` About Us    Contact Us    Legal    feedback    Copyright © 2015 - All Rights Reserved - www.4answered.com 'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),_trfd.push({'tccl.baseHost':'secureserver.net'}),_trfd.push({'ap':'cpsh'},{'server':'p3plcpnl0831'}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.```