# Generating a list with duplicate entries

How can I get a function that gives the following output?

listX = {a, b, c, d}
numberX = 3
myDuplicatesList[listX, numberX]


{{a,a,a},{b,b,b},{c,c,c},{d,d,d}}

Alternatively:

listX = {a, b, c};
numberX = 3;
Table[ConstantArray[i, numberX], {i, listX}]


{{a, a, a}, { b, b, b}, {c, c, c}}

Or:

listX = {a, b, c, d};
numberX = 3;

Transpose@Table[i, {numberX}, {i, listX}]

• Or ConstantArray[#, numberX] & /@ listX – Bob Hanlon Mar 8 at 16:57
• No need to use Transpose, just invert the order of the arguments: Table[i, {i,listX}, numberX] – jjagmath Mar 9 at 12:18

I like using KroneckerProduct for problems like this:

KroneckerProduct[listX, ConstantArray[1, numberX]]


{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}

The KroneckerProduct approach should be much faster than the others for large vectors.

one way might be

listX    = {a, b, c, d}
numberX  = 3
Transpose[{listX}].{Table[1, {numberX}]}


Transpose@ConstantArray[listX, numberX]


{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}

Array[listX &, numberX, 1, Transpose[{##}] &]


{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}

ArrayPad[List /@ listX, {0, {0, numberX - 1}}, "Fixed"]


{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}

PadRight[{#}, numberX, "Fixed"] & /@ listX  )* or *)
PadRight[List /@ listX, {Automatic, numberX}, "Fixed"]


{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}

TensorProduct[listX, Array[1 &, numberX]]


{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}

ArrayResample[listX, Scaled @ numberX , "Bin", Resampling -> "NearestLeft"]


{a, a, a, b, b, b, c, c, c, d, d, d}

Partition[%, numberX]


{{a, a, a}, {b, b, b}, {c, c, c}, {d, d, d}}

Yet Another Way:

Flatten[ConstantArray[{listX}, numberX], {2, 3}]


And another (the last argument 1 is necessary only when listX is not a flat list):

Outer[Times, listX, ConstantArray[1, numberX], 1]

Transpose[{listX}[[ConstantArray[1, numberX]]]]