Dorilds Dorilds - 4 months ago 16
Python Question

Removing specific items from a list but keeping some in python

I have a large data set that produces a list after using the re.finditer function to find all instances of a certain character. Sample list shown here:

[41, 64, 87, 105, 713, 736, 759, 777, 1385, 1408, 1431, 1449, 2057, 2080,
2103, 2121, 2729, 2752, 2775, 2793,...]


I need to delete all but every 4th item. So I need to delete the character at index 41, 64, and 87 but not 105. Delete 713, 736, and 759 but not 777. etc.

I am using python on a Mac OS 10.12.

UPDATE:

So now I have this new list
a=[105,777,1449,2121,2793]
and I wish to replace the indices of a textfile which I have imported into a variable. Could I just do:

for idx, item in enumerate(a):
raw_text[item] = "new character/string"

Answer Source

Based on your description, you want to remove all but every fourth element. You can do this with a slicing operator:

data[3::4]

Here 3 is the start index since the first index we are interested in, is at 3. The 4 means that we take hops of 4.

This generates:

>>> data[3::4]
[105, 777, 1449, 2121, 2793]

In case you are working with an iterable (not a list, tuple,...), you can use itertools.islice:

from itertools import islice

islice(data,3,None,4)

Here the None is semantically used as the stop index. Since we don't want to stop at a certain index, we use None. This will generate:

>>> list(islice(data,3,None,4))
[105, 777, 1449, 2121, 2793]