2
$\begingroup$

Is there a neat way to partition a list into sublists of fixed length $n$, discarding a fixed number $k$ elements between each one?

E.g for $n=3, k=1$,

f[Range[1,11],3,1]={{1,2},{4,5},{7,8},{10,11}}

I can do it with

Take[#, 2] & /@ Partition[pts, 3]

(or wrapped into a function) but I was expecting Partition to be able to do it in one call.

My use-case has a list of length <10,000, splitting into chunks of 100 with a small offset of 5 or so, so speed is not really an issue for me, but it is always interesting to see how to do these things quickly.

$\endgroup$
  • 4
    $\begingroup$ Partition[Range[1, 11], 2, 3]. From the docs (Generalizations and extensions): Skip elements by using an offset larger than the block size $\endgroup$ – user1066 Oct 18 '18 at 9:47
  • $\begingroup$ Thanks @user1066. Should have studied the docs more carefully $\endgroup$ – KraZug Oct 18 '18 at 9:49

Your Answer

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

Browse other questions tagged or ask your own question.