# Merging two lists by prepending elements

Being a newcomer to Mathematica, I have the following question.

Given

x = Range@9~Partition~3;


and

y = {a, b, c};


I want to combine x and y so that

result = {{a, 1, 2, 3}, {b, 4, 5, 6}, {c, 7, 8, 9}};


I have written

MapPrepend[a_, b_] := Prepend[a[[#]], b[[#]]] & /@ Range@Length@b


which does the job, but I wonder if there are other, more functional solutions?

Probably this question has already been answered. In this case it would be very nice if you just would direct me to the links.

• Join[{y}, Transpose[x]] // Transpose?
– kglr
May 14, 2014 at 18:58
• Please take a look here, there is something for everyone: mathematica.stackexchange.com/questions/3069/… May 14, 2014 at 18:59
• @kguler - thanks, short and elegant (sometimes you don't see the wood for the trees). Should also be faster on large lists;)
– eldo
May 14, 2014 at 19:10
• MapThread[Prepend, {x, y}]
– Kuba
May 14, 2014 at 21:45

I would simply do Flatten /@ Transpose[{y, x}].

• also very nice, thank you. To not forget it I just added to my init.m a "MapFlatten".
– eldo
May 14, 2014 at 19:29

Unfortunately, in terms of speed there is probably not much to gain here. However, I guess this is fancy

Join[List /@ y, x, 2]

• The shortest characterwise. Didn't know that "Join" accepts a level parameter.
– eldo
May 14, 2014 at 19:36
• @eldo copy this: {y, ## & @@ x}\[Transpose]
– Kuba
May 14, 2014 at 21:01
• @Kuba I can't beat that :). My shortest: {y, x /. _@z__ -> z}\[Transpose] May 14, 2014 at 21:34
• @JacobAkkerboom nice syntax :) it's interesting to see others habits :)
– Kuba
May 14, 2014 at 21:40