6
$\begingroup$

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},...}
$\endgroup$

13 Answers 13

7
$\begingroup$

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
$\endgroup$
2
  • $\begingroup$ Is it possible to iterate this command 5 times? $\endgroup$ Apr 11, 2021 at 20:08
  • $\begingroup$ @AgataBielecka Updated the answer to show one way to iterate. $\endgroup$ Apr 14, 2021 at 1:20
16
$\begingroup$

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 *)
$\endgroup$
2
  • 2
    $\begingroup$ Of course ! ;) This is the most simple answer. $\endgroup$
    – SquareOne
    Apr 12, 2021 at 20:24
  • 4
    $\begingroup$ I was wrong; THIS is the most direct way to do it ;). $\endgroup$ Apr 13, 2021 at 7:03
9
$\begingroup$
Riffle[data[[;; ;; 3]], data[[2 ;; ;; 3]]]
$\endgroup$
6
$\begingroup$

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

Delete[data, List /@ Range[3, Length[data], 3]]
$\endgroup$
4
$\begingroup$

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]}];

Mathematica graphics

And now use the new index

data[[idx]]

Mathematica graphics

$\endgroup$
4
$\begingroup$

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]]]]
$\endgroup$
1
  • $\begingroup$ Or data[[NestList[a=1;i=1; a|->a+ RotateRight[{2,1},i++][[1]],a,\[LeftCeiling]2/3 Length[data]\[RightCeiling]-1]]] $\endgroup$
    – Rabbit
    Apr 14, 2021 at 17:30
3
$\begingroup$

Using Partition, padded with Nothing

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

If it's OK to overwrite data, then simply

data[[3 ;; ;; 3]] = Nothing;
$\endgroup$
2
$\begingroup$
Table[
  If[Mod[n, 3] != 0, data[[n]], Nothing], {n, 1, Length[data]}]
$\endgroup$
2
  • $\begingroup$ This approach was already mentioned in Nasser's answer above $\endgroup$
    – MarcoB
    Apr 12, 2021 at 0:33
  • $\begingroup$ Almost. He used the extra step of generating the indices and then using those to select the elements of the data list. $\endgroup$
    – jmm
    Apr 13, 2021 at 0:27
2
$\begingroup$

My two answers :-)

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

and

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

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}}

$\endgroup$
1
$\begingroup$
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}}

$\endgroup$
1
$\begingroup$

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}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.