3
$\begingroup$

I have a list, say $\{1,2,3,4\}$ and I would like to repeat this list k times to obtain a list of $4k$ elements that looks like $\{1,2,3,4,1,2,..,1,2,3,4\}$.

I was hoping something like li * k would do this but unfortunately that's pointwise multiplication. What's the right operator that would do something like this for me?

$\endgroup$
0

6 Answers 6

7
$\begingroup$
list = {1, 2, 3, 4};

a = Flatten @ Table[list, 4]

{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}

b = Round @ Flatten @ ReplicateLayer[4, 1] @ list

{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}

c = Round @ Flatten @ ReplicateLayer[4, 2] @ list

{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4}

ReplicateLayer packs, Table doesn't:

Developer`PackedArrayQ /@ {a, b, c}

{False, True, True}

what-is-a-mathematica-packed-array

One can easily chain layers:

rep = Round @* ReplicateLayer[2,1] @* Flatten @* ReplicateLayer[2, 1];

rep @ list // MatrixForm

enter image description here

$\endgroup$
5
$\begingroup$

"I was hoping something like li * k would do this ..."

ConstantArray + Splice

ClearAll[Star]

Star[l_, k_] := ConstantArray[Splice @ l, k]

{1, 2, 3, 4}⋆3
 {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}

Use Table instead of ConstantArray to get the same result.

PadRight

ClearAll[Diamond]

Diamond[l_List, k_Integer] := PadRight[l, k Length@l, l]

{1, 2, 3, 4}⋄3
 {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}

Note: You can use any of the Operators without Built-in Meanings instead of \[Star] and \[Diamond].

Note: Although still undocumented, PadRight[l, k Length@l, "Periodic"] also works.

$\endgroup$
3
  • 1
    $\begingroup$ How does one discover hidden "options" like "Periodic"? Also, hopefully no one ever needs to use "Periodic" as their padding :) . I usually use the list itself as padding PadRight[list, length, list]. $\endgroup$
    – lericr
    Commented Nov 12, 2023 at 17:37
  • $\begingroup$ @lericr, discovered "Periodic" in the third argument of PadRight/PadLeft by the method of wishful thinking + luck:) I agree PadRight[list, length, list] is woker. $\endgroup$
    – kglr
    Commented Nov 12, 2023 at 17:48
  • $\begingroup$ Excellent! Thanks! $\endgroup$
    – lericr
    Commented Nov 12, 2023 at 17:52
3
$\begingroup$

Maybe we can get 10 ways to do this. Just to the add to the list of fun ways to do this using fun function names:

𒓬[l0_, k_] := Nest[Join[#, l0] &, {}, k]

𒓬[Range@4, 3]

(*{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}*)
$\endgroup$
3
$\begingroup$
SubstitutionSystem[MapApply[Rule]@Partition[Range[#1],
  2,1,1],1,(#1 #2)-1]&[4,3]//Flatten

(* {1,2,3,4,1,2,3,4,1,2,3,4} *)


SubstitutionSystem[MapApply[Rule]@Partition[Range[#1],
  2,1,1],1,(#1 #2)-1]&[10,3]//Flatten

(* {1,2,3,4,5,6,7,8,9,10,
    1,2,3,4,5,6,7,8,9,10,
    1,2,3,4,5,6,7,8,9,10} *)
$\endgroup$
3
$\begingroup$

Using ConstantArray and Cases:

f[lst_, k_] := Cases[ConstantArray[lst, k], x_ :> Sequence @@ x]

f[list, 4]

(*{1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}*)
$\endgroup$
3
$\begingroup$
list = {1, 2, 3, 4};
k = 4;
ArrayReshape[list, {Length[list] k}, list]

 (* {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4} *)

Flatten@Array[list &, k]

 (* {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4} *)
```
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.