5
$\begingroup$

I have a list

{{1, 2, 3, 4}, {1, 2, 4, 3}, {1, 3, 2, 4}, {1, 3, 4, 2}, {1, 4, 2,3},
{1, 4, 3, 2}, {2, 1, 3, 4}, {2, 1, 4, 3}, {2, 3, 1, 4}, {2, 3,4, 1},
{2, 4, 1, 3}, {2, 4, 3, 1}, {3, 1, 2, 4}, {3, 1, 4, 2}, {3, 2, 1, 4},
{3, 2, 4, 1}, {3, 4, 1, 2}, {3, 4, 2, 1}, {4, 1, 2, 3}, {4, 1, 3, 2},
{4, 2, 1, 3}, {4, 2, 3, 1}, {4, 3, 1, 2}, {4, 3, 2, 1}}

I want to make them this way- {{{1, 2}, {3, 4}}, {{1, 2}, {4, 3}},...

Which code should I use?

$\endgroup$
2
  • 5
    $\begingroup$ ArrayReshape, Partition etc. $\endgroup$ – Kuba Aug 20 '17 at 9:52
  • 1
    $\begingroup$ list // {#[[All, ;; 2]], #[[All, -2 ;;]]} & // Transpose and TakeDrop[#, 2] & /@ list are other possibilities. $\endgroup$ – user1066 Aug 20 '17 at 12:40
6
$\begingroup$

Besides Partition and Map (/@), you can also use ArrayReshape:

ArrayReshape[list, {Length@list, 2, 2}]
$\endgroup$
5
$\begingroup$

One way is to partition each element in your list into a list of two elements:

Partition[#, 2] & /@ list

Where list is your example list.

$\endgroup$
5
$\begingroup$
list = {{1, 2, 3, 4}, {1, 2, 4, 3}, {1, 3, 2, 4}};

list /. {a_, b_, c_, d_} :> {{a, b}, {c, d}}

or

{{#1, #2}, {#3, #4}} & @@@ list

both give

{{{1, 2}, {3, 4}}, {{1, 2}, {4, 3}}, {{1, 3}, {2, 4}}}

$\endgroup$

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.