# Partition of lists with specified elements

I have an array like this and want to split it into sublists. It should contain the first element "Res" without curly braces and the one sublist starts from 2 to the 9th column the 3rd should start from 10th column to 16th.

• I tried TakeList[#, {1, 8, 9}] & /@ list but it makes the first element also a sublist which I do not want,
– Miss
Mar 30 at 20:00
• As was mentioned on your earlier post, provide your data in copy and paste-able form so that we don't have to retype it. Mar 30 at 21:00
• Welcome to the Mathematica Stack Exchange. If you have started learning Mathematica, then you will find that the introductory book written by the inventor is a good learning resource. There is a fast intro for math students as well as a fast intro for programmers to choose from.
– Syed
Mar 31 at 13:18

Let's walk through this in a bit of detail. First we need some data, and since you haven't provided any in copy-paste-able form, I'll make some up:

list = {Take[Alphabet[], 16], Take[Alphabet[], -16]}


{{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"}, {"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}}

You said you tried TakeList[#, {1, 8, 9}] & /@ list, but since you only have 16 elements in each list (unless I miscounted), that will generate error messages. There are two remedies. First, match the length of your input list exactly:

TakeList[#, {1, 8, 7}] & /@ list


Alternatively, use All to finish off (this would be nice if you don't always know the exact length):

newList = TakeList[#, {1, 8, All}] & /@ list


(I assigned this to the variable newList so we can use it later.)

Both of these produce something that looks like this:

{{{"a"}, {"b", "c", "d", "e", "f", "g", "h", "i"}, {"j", "k", "l", "m", "n", "o", "p"}},
{{"k"}, {"l", "m", "n", "o", "p", "q", "r", "s"}, {"t", "u", "v", "w", "x", "y", "z"}}}


Okay, now you don't want those first elements to be a list--that's a job for FlattenAt. We want to flatten at the first position, and we want to do it for all lists in our newList structure. So, we'll use Map again:

FlattenAt[1] /@ newList


{{"a", {"b", "c", "d", "e", "f", "g", "h", "i"}, {"j", "k", "l", "m", "n", "o", "p"}}, {"k", {"l", "m", "n", "o", "p", "q", "r", "s"}, {"t", "u", "v", "w", "x", "y", "z"}}}

• Thanks! it works but at the same time gives me an error. Too few arguments given at FlattenAt.
– Miss
Mar 31 at 9:11
• What's the exact error message (including the tag)? What version of Mathematica are you using (FlattenAt has been around a long time, but might as well cover our bases). And did you run the exact code I provided above? In that sequence? You didn't skip ahead to trying to change it for your own data before verifying that the code worked as provided? Mar 31 at 13:59
• I wonder how long the operator form of FlattenAt has been available? Nothing documented about this it would seem, but maybe FlattenAt[#,1]&/@newList would work? Mar 31 at 14:40
• @user1066 good suggestion. OP also said "it works", so I'm confused. Will wait for more info before updating the answer. Mar 31 at 15:22

Do you require something like the following?

Through[{First, Splice@TakeList[Rest@#,{8,7}]&}[#]]&/@list2

(*
{
{a, {b, c, d, e, f, g, h, i}, {j, k, l, m, n, o, p}},
{k, {l, m, n, o, p, q, r, s}, {t, u, v, w, x, y, z}}
}
*)


Or, taking the example data from your previous question:

Through[{First, Splice@TakeList[Rest@#,{8,7}]&}[#]]&/@list

(*
{
{Res , {Around[2.5, 0.03], Around[1.5, 0.66], 0.07,
Around[2.76, 0.357], Around[6.736, 2.14],
Around[4.76, 0.8732], Around[12.7, 1.08],Around[33.7, 3.747]},
{50, 0.08, 0.01, 1. 10^-8  , 4, 1, 1}
},

{Res, {Around[2., 0.07], Around[1.5, 0.01], Around[0.21, 0.06],
Around[2.823, 0.68], Around[6.5965, 2.49], Around[3.874, 1.],
Around[11.34, 1.1], Around[24.3, 3.02]},
{50, 0.08, 0.01, 1, 4.8, 1, 1}}
}

*)


where list2 is taken from lericr's answer, and list is taken from your previous question

list2 = {Take[Alphabet[], 16], Take[Alphabet[], -16]}

• Hello everyone. Thank you for the help. It worked out for me.
– Miss
Mar 31 at 16:26
• And, of course, FoldPairList/TakeDrop may be substituted for TakeList. For example: Through[{First@#&, Splice@FoldPairList[TakeDrop,Rest@#,{8,7}]&}[#]]&/@list2 Apr 1 at 9:51