4
$\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$

10 Answers 10

4
$\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$ – Agata Bielecka Apr 11 at 20:08
  • $\begingroup$ @AgataBielecka Updated the answer to show one way to iterate. $\endgroup$ – Rohit Namjoshi Apr 14 at 1:20
14
$\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 at 20:24
  • 4
    $\begingroup$ I was wrong; THIS is the most direct way to do it ;). $\endgroup$ – Sjoerd Smit Apr 13 at 7:03
8
$\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$ – Christopher Lamb Apr 14 at 17:30
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 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 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$
2
$\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 at 18:53
  • $\begingroup$ @user1066 Thanks, very interesting. I also learned from xzczd's double span answer. $\endgroup$ – Chris Degnen Apr 14 at 10:26
  • 1
    $\begingroup$ Slightly shorter, lol Join@@Partition[data, UpTo[2], 3] $\endgroup$ – Chris Degnen Apr 14 at 10:39
2
$\begingroup$
newdata = data;
newdata[[3 ;; ;; 3]] = Nothing;
newdata

If it's OK to overwrite data, then simply

data[[3 ;; ;; 3]] = Nothing;
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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