Get a List With `Partition`
Clear["Global`*"]
$HistoryLength = 0;
The two methods produce identical results
With[{n = 1000},
List /@ Partition[Range[n], 2] ===
ArrayReshape[Range[n], {n/2, 1, 2}]]
(* True *)
Comparing timings
timing1[n_Integer?Positive] :=
RepeatedTiming[List /@ Partition[Range[n], 2];][[1]]
timing2[n_Integer?Positive] :=
Module[{data = Range[n]},
RepeatedTiming[ArrayReshape[data, {Length[data]/2, 1, 2}];][[1]]]
ListLogLogPlot[{
{#, timing1[#]} & /@ (10^Range[7]),
{#, timing2[#]} & /@ (10^Range[7])},
Joined -> True,
Frame -> True,
FrameLabel -> (Style[#, 14, Bold] & /@
{"number of elements",
"timing"}),
PlotLegends ->
Placed[{"Partition and Map", ArrayReshape}, {.35, .75}]]
Slower than ArrayReshape
but ... there is also
BlockMap
BlockMap[List, Range@18, 2]
{{{1, 2}}, {{3, 4}}, {{5, 6}}, {{7, 8}}, {{9, 10}}, {{11, 12}}, {{13, 14}}, {{15, 16}}, {{17, 18}}}
and the (undocumented) 6-argument form of Partition
:
Partition[Range@18, 2, 2, 1, {}, {{##}} &]
{{{1, 2}}, {{3, 4}}, {{5, 6}}, {{7, 8}}, {{9, 10}}, {{11, 12}}, {{13, 14}}, {{15, 16}}, {{17, 18}}}
Possibly what you are looking for:
First @ Partition[{Range[18]}, {1, 2}]
{{{1, 2}}, {{3, 4}}, {{5, 6}}, {{7, 8}}, {{9, 10}}, {{11, 12}}, {{13, 14}}, {{15, 16}}, {{17, 18}}}
Note the extra { }
around Range[18]
to make it two dimensional.
Or Partitioning sequentially:
Fold[Partition, Range[18], {2, 1}]