# Prepend multiple values to a list

If I have a list such as list={1,2,3,4,5}, how can a prepend multiple values to the list?. For example what I want is to prepend 6 zeros prior to {1,2,3,4,5} to get {0,0,0,0,0,0,1,2,3,4,5}. I tried using Prepend[list, Table[0, 6]] but this give me {{0,0,0,0,0,0},1,2,3,4,5} which would be all the six zeros as the first element rather than each zero being an element.

Thank you.

You have lots of options, such as:

list = {1, 2, 3, 4, 5};
zeros = ConstantArray[0, 3];

Catenate[{zeros, list}]

(* or *)

Join[zeros, list]

(* or *)

Flatten[{zeros, list}, 1]


{0, 0, 0, 1, 2, 3, 4, 5}

As of Mathematica 12.1, you also have this option:

{Splice[zeros], Splice[list]}


{0, 0, 0, 1, 2, 3, 4, 5}

Which I guess is equivalent to doing this, which also works in earlier versions:

{Sequence @@ zeros, Sequence @@ list}


{0, 0, 0, 1, 2, 3, 4, 5}

But Splice has the additional advantage that it is not evaluated before it occurs in the context of a list, which means that we can use it with Prepend, and salvage your initial attempt:

Prepend[list, Splice[zeros]]


{0, 0, 0, 1, 2, 3, 4, 5}

(Prepend[list, Sequence @@ zeros] would not have have worked since Sequence would have been applied immediately, which nicely highlights the utility of the Splice.)

• Thank you very much !
– John
Commented Apr 30, 2020 at 22:51
• You can of course do Prepend[{1, 2, 3, 4, 5}, Unevaluated[Sequence @@ {0, 0, 0}]], but Splice[] is surely easier to handle. Commented May 2, 2020 at 14:22
• @J.M. good point, thanks. Commented May 2, 2020 at 16:34

It sounds like you want to Join two lists.

• Thank you @Szabolcs! That's exactly what I was looking for.
– John
Commented Apr 30, 2020 at 22:32

You can also use ArrayPad and PadLeft:

ArrayPad[list, {6, 0}]

{0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5}

PadLeft[list, 6 + Length@list]

 {0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5}