Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

How to partition a list and leave in the last sublist which is of different length?

In[75]:= Partition[{1,2,3,4,5},2]
Out[75]= {{1,2},{3,4}}

I want it to be

{{1,2},{3,4},{5}}
share|improve this question

marked as duplicate by Mr.Wizard May 12 '13 at 19:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4  
This is easily answered by the documentation: Partition[{1, 2, 3, 4, 5}, 2, 2, {1, 1}, {}] –  rm -rf Dec 16 '12 at 16:39
    
Em ... sorry that somehow my scan through the doc didn't catch this. –  qazwsx Dec 16 '12 at 16:49
    
OK, so you don't mind if this is closed as Too Localized? –  Sjoerd C. de Vries Dec 16 '12 at 16:51
7  
@rm-rf @Sjoerd This is a very common problem though, and it has a somewhat unexpectedly inconvenient syntax. One really has to read through the Partition docs fully to find it, otherwise it's not obvious that Partition can even do this. I would not close or delete the question, but post the answer instead. –  Szabolcs Dec 16 '12 at 17:38
1  
See also this question for some more elaborate partition schemes. –  Mr.Wizard Dec 16 '12 at 23:36

2 Answers 2

up vote 7 down vote accepted

You can use the additional arguments of Partition to achieve this result. The first 5 arguments of Partition are (see the docs for more info):

  • list: The list to be partitioned
  • n: Length of the sublists (except perhaps for sublists with insufficient elements). Should be less than Length@list
  • d: Partition offset. By default this is the same as n (no overlaps). A smaller value will result in overlaps of n-d samples and a larger value will result in skipping of d-n samples after every n samples.
  • {kL, kR}: Determines whether overhangs are allowed at the beginning or the end of the list
  • x: If overhangs are allowed, sublists with insufficient elements are padded with x.

Using the above, you can get your desired output with:

Partition[{1, 2, 3, 4, 5}, 2, 2, {1, 1}, {}]
(* {{1, 2}, {3, 4}, {5}} *)
share|improve this answer
2  
I think it's simpler to use Partition[{1, 2, 3, 4, 5}, 2, 2, 1, {}] –  Murta Dec 16 '12 at 22:59
1  
@Murta Yes, but I didn't think this was the question to mention non-pair values for the overhang parameter, especially since the OP didn't know what they were (btw, for the sake of completeness, they can take values other than ±1 too, as in this answer, although the docs don't mention it) –  rm -rf Dec 16 '12 at 23:39
    
@rm-rf Do you know how to partition with the last two list combined? For example {1, 2, 3, 4, 5} to {{1, 2}, {3, 4, 5}}, or {1,2,3,4,5,6,7,8} to {{1,2,3},{4,5,6,7,8}}. Thanks. –  xslittlegrass Oct 22 at 1:55
    
@xslittlegrass You can post-process the output with a rule. Something like list /. {h___List, p_List, l_List} :> {h, p ~Join~ l} –  rm -rf Oct 22 at 2:56
    
@rm-rf OK that works, thanks :) –  xslittlegrass Oct 22 at 3:16

A useful Manipulate can help understanding how Partition works. It also provides the code to use for a certain partitioning.

Manipulate[
 Grid[{
   {"original list:", Range[n]},
   {},
   {"without offset:", Partition[Range[n], part]},
   {"code:", Style[With[{n = n, part = part}, HoldForm@Partition[Range[n], part]], Bold]},
   {},
   {"with offset:", Partition[Range[n], part, offset, pos, pad]},
   {"code:", Style[With[{n = n, part = part, offset = offset, pos = pos, pad = pad}, 
      HoldForm@Partition[Range[n], part, offset, pos, pad]], Bold]}
   }, Alignment -> {{Right, Left}}, Spacings -> {1, 1}],
 Item[Style["Understanding Partition", FontFamily -> "Helvetica", Bold, 18]],
 Delimiter,
 {{n, 9, "length"}, 0, 20, 1, Appearance -> "Labeled"},
 {{part, 1, "partition size"}, 1, 20, 1, Appearance -> "Labeled"},
 {{offset, part, "offset"}, 1, part, 1, Appearance -> "Labeled"},
 {{pos, {1, 1}, "position"}, {{1, -1}, {1, 1}, {-1, -1}, {-1, 1}}, ControlType -> SetterBar},
 {{pad, "X", "padding with"}, {{} -> "{}", 0, 1, a, "X", Graphics[{Blue, Disk[]}, ImageSize -> 12]}, ControlType -> SetterBar}
 ]

Understanding Partition

share|improve this answer

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