Split list into overlapping sublists

I would like to split a list into overlapping sublists! I want to generate a first sublist with a specified number of elements. Then I want to shift one element to the right and generate an overlapping sublist. ... and so on, until the end. All sublists should have the same length. Just as an example:

{1, 6, 3, 6, 8, 5, 3}


I would like to obtain the following:

{{1, 6, 3, 6}, {6, 3, 6, 8}, {3, 6, 8, 5}, {6, 8, 5, 3}}


I have no idea how to tackle the problem. Thanks in advance.

• The first sublist here has length 4, and the others have length 5. Are you sure you meant that? Aug 18, 2015 at 14:13
• I corrected the question! You are right.
– Niki
Aug 18, 2015 at 14:16

Partition[{1, 6, 3, 6, 8, 5, 3}, 4, 1]


is probably what you want. It returns:

{{1, 6, 3, 6}, {6, 3, 6, 8}, {3, 6, 8, 5}, {6, 8, 5, 3}}

• Thanks! I am a bit embarrassed that I didn't figure that out myself.
– Niki
Aug 18, 2015 at 14:16
• These things are obvious when you know the answer. Aug 18, 2015 at 14:17
• I mean it was written in the documentation. I just misunderstood what is said there. Thanks anyway.
– Niki
Aug 18, 2015 at 14:21
• @Niki, Could I just nudge you to accept this answer, if it's all sorted? I don't like seeing those horrible grey squares in my "answers" list on my user page :P Aug 21, 2015 at 11:10
list = {1, 6, 3, 6, 8, 5, 3};


Using SequenceCases (new in 10.1)

SequenceCases[list, {Repeated[_, {4}]}, Overlaps -> True]


{{1, 6, 3, 6}, {6, 3, 6, 8}, {3, 6, 8, 5}, {6, 8, 5, 3}}

An alternative is to use ListCorrelate and CoefficientArrays:

list = {1, 6, 3, 6, 8, 5, 3};

Normal@CoefficientArrays[ListCorrelate[{x, y, z, w}, list], {x, y, z, w}][[2]]

(*{{1, 6, 3, 6}, {6, 3, 6, 8}, {3, 6, 8, 5}, {6, 8, 5, 3}}*)


Or using Table:

With[{n = 4, s = 1},
Table[#[[i ;; i + n - 1]], {i, 1, Length@# - n + 1, s}] &@list]

(*{{1, 6, 3, 6}, {6, 3, 6, 8}, {3, 6, 8, 5}, {6, 8, 5, 3}}*)