7
$\begingroup$

I use the function Drop quite often. When looking at this question, my attention was caught by a use of Drop in a way I did not see before. So I read the documentation and found the following description:

Drop[list,{m,n,s}] gives list with elements m through n in steps of s dropped.

As I read it, this formulation suggests that the list is partioned in sublists of length s and that in each of these sublists the elements m through n are dropped.

Drop[Range[10], {2,3,5}]
=> {1,3,4,5,6,7,8,9,10}

I expected that the elements 2, 3, 7 and 8 would be dropped. It is only element 2 that is dropped. I get the same result when I replace the number 3 (n) with any number between 2 and 10. So with these values, m through n seems to result in only m for the original list.

In the next command I indeed see some periodictity with respect to s:

Drop[Range[10], {3,-1, 5}]
=> {1, 2, 4, 5, 6, 7, 9, 10}

But I fail to see why elements 3 to -1 in steps of 5 results in 3 and 8. I get the same result when I replace -1 with -2 or -3.

Finally:

Drop[Range[10], {5,-1, 5}]
=> {1,2,3,4,6,7,8,9,10}

Here elements 5 and 10 are dropped. But:

Drop[Range[10], {5,-2, 5}]
=> {1,2,3,4,6,7,8,9,10}

Now only element 5 is dropped.

Can someone give me a better understanding how I can predict the result of Drop, when used with a list {m, n, s}?

$\endgroup$
  • 1
    $\begingroup$ Another way to write it: For m < n (and s > 0 by necessity), Drop[list, {m, n, s}] drops m + s*i for all s such that m + s*i <= n. If n > m we must have that s < 0 and it will drop m + s*i for all s such that m + s*i >= n. $\endgroup$ – C. E. Mar 14 '17 at 11:59
8
$\begingroup$

It works analogously to Span:

list = Range[10];
spec = 3 ;; -1 ;; 5;

MapAt[Style[#, Red] &, list, spec]
list[[spec]]
Drop[list, spec]
Drop[list, List @@ spec] (*your case*)

enter image description here

$\endgroup$
  • 1
    $\begingroup$ @C. E. Thanks, that makes it clear. In the documentation, 'elements m through n in steps of s', the steps refer only to m, not to n. Now and then it is a disadvantage being a mathematician. $\endgroup$ – Fred Simons Mar 14 '17 at 12:51
  • 1
    $\begingroup$ there are negative m,n examples in the docs if you drill down to scope. $\endgroup$ – george2079 Mar 14 '17 at 16:02

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.