# Combining list elements

I am doing the following:

SeedRandom[1];
n = 3;
data = {RandomInteger[10], RandomInteger[10, {4, 5}]} & /@ Range[n]

{{1, {{4, 0, 7, 0, 0}, {8, 6, 0, 4, 1}, {8, 5, 1, 1, 1}, {3, 2, 10, 1, 6}}},
{0, {{2, 6, 4, 5, 4}, {3, 0, 1, 3, 5}, {3, 0, 3, 2, 3}, {9, 5, 1, 5, 2}}},
{3, {{9, 1, 0, 4, 4}, {1, 5, 2, 7, 9}, {9, 8, 10, 0, 10}, {10, 7, 4, 9, 2}}}}


The result should be:

Flatten[Table[Append[#, data[[i, 1]]] & /@ data[[i, 2]], {i, n}], 1]

{{4, 0, 7, 0, 0, 1}, {8, 6, 0, 4, 1, 1}, {8, 5, 1, 1, 1, 1}, {3, 2, 10, 1, 6, 1},
{2, 6, 4, 5, 4, 0}, {3, 0, 1, 3, 5, 0}, {3, 0, 3, 2, 3, 0}, {9, 5, 1, 5, 2, 0},
{9, 1, 0, 4, 4, 3}, {1, 5, 2, 7, 9, 3}, {9, 8, 10, 0, 10, 3}, {10, 7, 4, 9, 2, 3}}


Can you propose another FAST solution instead of using Table?

• Append @@@ Join @@ (Thread /@ Reverse /@ data)?
– kglr
Oct 12, 2017 at 14:44
• @kglr: wau, please put this into an answer ... this is incredably fast
– mrz
Oct 12, 2017 at 14:55

Append @@@ Join @@ (Thread /@ Reverse /@ #) & @ data


{{4, 0, 7, 0, 0, 1}, {8, 6, 0, 4, 1, 1}, {8, 5, 1, 1, 1, 1}, {3, 2, 0, 1, 6, 1}, {2, 6, 4, 5, 4, 0},
{3, 0, 1, 3, 5, 0}, {3, 0, 3, 2, 3, 0}, {9, 5, 1, 5, 2, 0}, {9, 1, 0, 4, 4, 3},
{1, 5, 2, 7, 9, 3}, {9, 8, 10, 0, 10, 3}, {10, 7, 4, 9, 2, 3}}

• thanks a lot for the really FAST solution.
– mrz
Oct 13, 2017 at 13:31

tomd was close (+1) but there is better:

ArrayFlatten[data ~Reverse~ 2]


Related: Prepend 0 to sublists

• That is very nice! Oct 13, 2017 at 11:45
– mrz
Oct 13, 2017 at 13:27
• Off-topic question:Are you also the "Mr. Wizard" on the Outdoors SE forum here? Oct 15, 2017 at 20:35
• @Daniel, yes, it's him. Oct 16, 2017 at 3:26
• @J.M. Okay, good to know. I had an unfortunate need for the sort of info at that link yesterday. Oct 16, 2017 at 3:32
ArrayFlatten[{#}] & /@ Reverse /@ data  // Catenate


{{4, 0, 7, 0, 0, 1}, {8, 6, 0, 4, 1, 1}, {8, 5, 1, 1, 1, 1}, {3, 2, 10, 1, 6, 1}, {2, 6, 4, 5, 4, 0}, {3, 0, 1, 3, 5, 0}, {3, 0, 3, 2, 3, 0}, {9, 5, 1, 5, 2, 0}, {9, 1, 0, 4, 4, 3}, {1, 5, 2, 7, 9, 3}, {9, 8, 10, 0, 10, 3}, {10, 7, 4, 9, 2, 3}}

See this answer, due to Janus, at SO.

Slight modification of above:

ArrayFlatten[{#}] & /@ Reverse[data, 2] // Catenate


Edit

Mr Wizard, in this answer, gives an elegant modification:

Reverse[data, 2] // ArrayFlatten


Just for fun:

Flatten /@ Tuples[{ #[[2]], {#[[1]]}}] & /@ data // Catenate

• Thank you ... this is also very fast and elegant too
– mrz
Oct 13, 2017 at 2:02
• ArrayFlatten is a really useful function. It's very good to try and get it into your repertoire. Oct 13, 2017 at 9:30

Try this:

BlockRandom[SeedRandom[1];
{{{4, 0, 7, 0, 0, 1}, {8, 6, 0, 4, 1, 1}, {8, 5, 1, 1, 1, 1},
{3, 2, 10, 1, 6, 1}},
{{2, 6, 4, 5, 4, 0}, {3, 0, 1, 3, 5, 0}, {3, 0, 3, 2, 3, 0}, {9, 5, 1, 5, 2, 0}},
{{9, 1, 0, 4, 4, 3}, {1, 5, 2, 7, 9, 3}, {9, 8, 10, 0, 10, 3}, {10, 7, 4, 9, 2, 3}}}

• thank you ... in my real data the number of elements can be anything and is not fixed (my example does not show it)
– mrz
Oct 12, 2017 at 14:58
• Should've made a more representative example, then. Oct 12, 2017 at 15:05
list =
{{1, {{4, 0, 7, 0, 0}, {8, 6, 0, 4, 1}, {8, 5, 1, 1, 1}, {3, 2, 10, 1, 6}}},
{0, {{2, 6, 4, 5, 4}, {3, 0, 1, 3, 5}, {3, 0, 3, 2, 3}, {9, 5, 1, 5, 2}}},
{3, {{9, 1, 0, 4, 4}, {1, 5, 2, 7, 9}, {9, 8, 10, 0, 10}, {10, 7, 4, 9, 2}}}};


MapApply came with V 13.1

Join @@ MapApply[Append @ #1 /@ #2 &] @ list


gives

{{4, 0, 7, 0, 0, 1}, {8, 6, 0, 4, 1, 1}, {8, 5, 1, 1, 1, 1}, {3, 2, 10, 1, 6, 1},
{2, 6, 4, 5, 4, 0}, {3, 0, 1, 3, 5, 0}, {3, 0, 3, 2, 3, 0}, {9, 5, 1,5, 2, 0},
{9, 1, 0, 4, 4, 3}, {1, 5, 2, 7, 9, 3}, {9, 8, 10, 0, 10, 3}, {10, 7,4, 9, 2, 3}}