# Mathematica values new list

I have a list:

data = {{2.*10^-9, 0.0025}, {4.*10^-9, 0.0025}, {6.*10^-9, 0.0025}, {8.*10^-9, 0.0025}, {1.*10^-8, 0.0025}, {7.*10^-9, 0.0023}, {3.*10^-9, 0.0025},...}


And I wanted to remove every third pair and get

 newdata = {{2.*10^-9, 0.0025}, {4.*10^-9, 0.0025}, {8.*10^-9, 0.0025}, {1.*10^-8, 0.0025}, {3.*10^-9, 0.0025},...}


Another way

MapIndexed[If[Divisible[First@#2, 3], Nothing, #1] &, data]


Update

One way to iterate is to use Nest.

filter = MapIndexed[If[Divisible[First@#2, 3], Nothing, #1] &, #] &;

data = Range[20]; (* Easy to see what is removed *)
Nest[filter, data, 5]
(* {1, 2, 14, 20} *)


To see intermediate steps

NestList[filter, data, 5] // Column

• Is it possible to iterate this command 5 times? Apr 11, 2021 at 20:08
• @AgataBielecka Updated the answer to show one way to iterate. Apr 14, 2021 at 1:20

There are many ways to do this, but my favorite is to use the the stride and end arguments in Drop:

Drop[data, {3, -1, 3}] === newdata
(* True *)

• Of course ! ;) This is the most simple answer. Apr 12, 2021 at 20:24
• I was wrong; THIS is the most direct way to do it ;). Apr 13, 2021 at 7:03
Riffle[data[[;; ;; 3]], data[[2 ;; ;; 3]]]


If you ask me, this is the most direct approach:

Delete[data, List /@ Range[3, Length[data], 3]]


I am sure there are many ways to do this. One direct way could be to build the index and use it to select the entries.

data = {{2.*10^-9, 0.0025}, {4.*10^-9, 0.0025}, {6.*10^-9,
0.0025}, {8.*10^-9, 0.0025}, {1.*10^-8, 0.0025}, {7.*10^-9,
0.0023}, {3.*10^-9, 0.0025}};


And now

idx = Table[If[Mod[n, 3] != 0, n, Nothing], {n, 1, Length[data]}];


And now use the new index

data[[idx]]


Another way to construct the needed indices:

data[[Union[Range[1, Length[data], 3], Range[2, Length[data], 3]]]]

{{2.*10^-9, 0.0025}, {4.*10^-9, 0.0025}, {8.*10^-9, 0.0025}, {1.*10^-8, 0.0025}, {3.*10^-9, 0.0025}}


Similarly:

data[[Complement[Range[Length[data]], Range[3, Length[data], 3]]]]

• Or data[[NestList[a=1;i=1; a|->a+ RotateRight[{2,1},i++][[1]],a,\[LeftCeiling]2/3 Length[data]\[RightCeiling]-1]]] Apr 14, 2021 at 17:30

Using Partition, padded with Nothing

Flatten[Take[#, UpTo[2]] & /@ Partition[data, 3, 3, {1, 1}, Nothing], 1]

• Also, maybe, Partition[data, UpTo[2],3]//Catenate Apr 13, 2021 at 18:53
• @user1066 Thanks, very interesting. I also learned from xzczd's double span answer. Apr 14, 2021 at 10:26
• Slightly shorter, lol Join@@Partition[data, UpTo[2], 3] Apr 14, 2021 at 10:39
newdata = data;
newdata[[3 ;; ;; 3]] = Nothing;
newdata


If it's OK to overwrite data, then simply

data[[3 ;; ;; 3]] = Nothing;

Table[
If[Mod[n, 3] != 0, data[[n]], Nothing], {n, 1, Length[data]}]

• This approach was already mentioned in Nasser's answer above Apr 12, 2021 at 0:33
• Almost. He used the extra step of generating the indices and then using those to select the elements of the data list.
– jmm
Apr 13, 2021 at 0:27

data[[Select[Range[Length[data]], Mod[#, 3] != 0 &]]]


and

Transpose[Select[Transpose[{data, Range[Length[data]]}], Mod[#[[2]], 3] != 0 &]][[1]]


Using ReplaceAt (new in 13.1)

ReplaceAt[data, _-> Nothing, 3 ;; ;; 3]


{{2.*10^-9, 0.0025}, {4.*10^-9, 0.0025}, {8.*10^-9, 0.0025}, {1.*10^-8, 0.0025}, {3.*10^-9, 0.0025}}

data = {{2.*10^-9, 0.0025}, {4.*10^-9, 0.0025},
{6.*10^-9, 0.0025}, {8.*10^-9, 0.0025},
{1.*10^-8, 0.0025}, {7.*10^-9, 0.0023},
{3.*10^-9, 0.0025}};


Using PositionIndex:

Keys[Select[PositionIndex[data], ! Divisible[#[[1]], 3] &]]


{{2.*10^-9, 0.0025}, {4.*10^-9, 0.0025}, {8.*10^-9, 0.0025}, {1.*10^-8, 0.0025}, {3.*10^-9, 0.0025}}

Using SequenceReplace:

data = {{2.*10^-9, 0.0025}, {4.*10^-9, 0.0025}, {6.*10^-9,
0.0025}, {8.*10^-9, 0.0025}, {1.*10^-8, 0.0025}, {7.*10^-9,
0.0023}, {3.*10^-9, 0.0025}};

SequenceReplace[data, {a_, b_, c_} :> Sequence @@ {a, b}]


Result:

{{2.*10^-9, 0.0025}, {4.*10^-9, 0.0025}, {8.*10^-9, 0.0025}, {1.*10^-8, 0.0025}, {3.*10^-9, 0.0025}}