1
$\begingroup$

I guess this is rather trivial. Given, e.g.,

L={1,2}

what are some common efficient ways to obtain

L
(*{1,2,1,2,1,2}*)

that is, the list concatenated three times with itself and obtained the new value. Thanks in advance.

$\endgroup$
  • 3
    $\begingroup$ Catenate@ConstantArray[L, 3]? $\endgroup$ – Szabolcs Apr 4 '17 at 19:59
  • $\begingroup$ closely related: 111863, 113327 related: 123743 $\endgroup$ – Kuba Apr 4 '17 at 20:01
  • 1
    $\begingroup$ Outer[Times, ConstantArray[1, 5], {1, 2}] // Flatten $\endgroup$ – bill s Apr 4 '17 at 20:05
  • 2
    $\begingroup$ Is this close enough for a duplicate? Generate cyclic list from a list $\endgroup$ – Kuba Apr 4 '17 at 20:10
  • 1
    $\begingroup$ Thanks for the comments and suggested workarounds. I accept the duplicate nature of the question but just as a side remark. I wrote: ""I guess this is rather trivial". After having seen the responses I think I should not have written this phrase. I use Mathematica for several years and still I could not find anything to do it on my oown. I use Python for one month and I am able to simply write 3*L to get the desired output:-)! $\endgroup$ – Dimitris Apr 5 '17 at 20:13
4
$\begingroup$
L = {1, 2};
Charting`padList[L, 6]

{1,2,1,2,1,2}


Ps:Function Charting`CommonDump`listPad,Catenate,Flatten,ReplicateLayer,PaddingLayer,Table,ConstantArray,ArrayReshape and Array is relevant.Also ArrayPad from Kuba's comment here.

| improve this answer | |
$\endgroup$
4
$\begingroup$

There is a dedicated function in R:

Needs["RLink`"]    
InstallR[]    
REvaluate["rep(c(1,2),5)"]

(* {1., 2., 1., 2., 1., 2., 1., 2., 1., 2.} *)

Another possibility is to use length.out:

REvaluate["rep(c(1,2),length.out=5)"]    

(* {1., 2., 1., 2., 1.} *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Upvote for trick.. $\endgroup$ – yode Apr 4 '17 at 20:18
  • $\begingroup$ @yode It is related to your answer, using a dedicated function. $\endgroup$ – Anton Antonov Apr 4 '17 at 20:19

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