12
$\begingroup$

I am trying to find how can do this really quick:

list = {{a, b}, c, {d, e}, f, {g, h}, i}

Desired output List is:

{{a,b,c},{d,e,f},{g,h,i}}

Looks like simple but it takes me plenty of time to think... sorry if this one has been asked before, I didn't find any answer yet.

$\endgroup$
2
  • $\begingroup$ BTW, you cannot set values to List. $\endgroup$ Dec 5, 2017 at 3:44
  • $\begingroup$ Yes, that's a typo, thanks for pointing it out! $\endgroup$
    – leon365
    Dec 5, 2017 at 14:10

4 Answers 4

15
$\begingroup$

Also:

Append @@@ Partition[list, 2]
ArrayReshape[list, {3, 3}]
SequenceReplace[{{x_, y__}, z_} :> {x, y, z}]@list
Flatten /@ Partition[list, 2] (* thanks: Okkes Dulgerci *)
{## & @@ #, #2} & @@@ Partition[list, 2]

all give

{{a, b, c}, {d, e, f}, {g, h, i}}

Update: You can also use the undocumented 6th argument of Partition:

Partition[list, 2, 2, {1, -1}, {}, Append]

{{a, b, c}, {d, e, f}, {g, h, i}}

$\endgroup$
5
  • 1
    $\begingroup$ Out of these 3 methods, I'm guessing that ArrayReshape would be the most efficient, right? $\endgroup$ Dec 5, 2017 at 10:52
  • $\begingroup$ Thank you! Great answers! $\endgroup$
    – leon365
    Dec 5, 2017 at 14:03
  • $\begingroup$ @SjoerdSmit, sorry for late response. Yes, based on limited tests ArrayReshape and Append+Partition combination are the fastest of the four methods. $\endgroup$
    – kglr
    Dec 20, 2017 at 5:13
  • $\begingroup$ Flatten /@ Partition[list, 2] $\endgroup$ Nov 10, 2018 at 2:06
  • $\begingroup$ Thank you @OkkesDulgerci. $\endgroup$
    – kglr
    Nov 10, 2018 at 21:35
8
$\begingroup$
BlockMap[Flatten, list, 2]

Of course, the BlockMap also can be Developer`PartitionMap

$\endgroup$
3
  • $\begingroup$ This should be the most concise one. $\endgroup$ Dec 5, 2017 at 8:49
  • $\begingroup$ Thank you, sir! $\endgroup$
    – leon365
    Dec 5, 2017 at 14:08
  • $\begingroup$ I had completely forgotten about this one. Thanks. $\endgroup$
    – Edmund
    Nov 10, 2018 at 1:44
7
$\begingroup$

If the input list is consistently dimensioned, then this would work:

list = {{a, b}, c, {d, e}, f, {g, h}, i};
Partition[ Flatten[list, 1], 3]

I hope that this gets you what you need.

$\endgroup$
2
  • $\begingroup$ yes, the pattern is consistent. Thanks for your help, this is work! $\endgroup$
    – leon365
    Dec 5, 2017 at 3:28
  • 4
    $\begingroup$ Partition[Flatten[list], 3] is enogh $\endgroup$
    – yode
    Dec 5, 2017 at 9:11
3
$\begingroup$

Method I

Step by step

Cases[list, _List]
List /@ Complement[list, %]
Join[%%, %, 2]

give respectively

{{a, b}, {d, e}, {g, h}}

{{c}, {f}, {i}}

{{a, b, c}, {d, e, f}, {g, h, i}}

Method II

Flatten /@ Transpose @ GatherBy[list, Head]
$\endgroup$
2
  • $\begingroup$ Wow, nice illustration! $\endgroup$
    – leon365
    Dec 5, 2017 at 14:04
  • $\begingroup$ @leon365 Glad to be helpful. :) $\endgroup$ Dec 5, 2017 at 14:09

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.