MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: repeating elements in a list

  • To: mathgroup at
  • Subject: [mg54931] Re: repeating elements in a list
  • From: Scott Hemphill <hemphill at>
  • Date: Sun, 6 Mar 2005 00:56:44 -0500 (EST)
  • References: <d0dekq$lek$>
  • Reply-to: hemphill at
  • Sender: owner-wri-mathgroup at

Torsten Coym <torsten.coym at> writes:

> Hello,
> suppose I have list
> lst={A, B, C}
> and I want to create a new list, where all elements are repeated n times 
> so that (for n=3):
> newlst={A, A, A, B, B, B, C, C, C}
> I have the following code to do this:
> n=3;
> newlst=Flatten[Table[Table[lst[[i]], {n}], {i, Length[lst]}]];
> but I'm pretty sure, that there must be a more elegant way to solve the 
> problem.

I would do it like so:

  Join @@ ( Table[#,{n}]& /@ lst )

Using Join instead of Flatten allows you to also make n copies of list
objects, if that is the desired behavior:

  In[1]:= lst = {{A,B},C,D}

  Out[1]= {{A, B}, C, D}

  In[2]:= n=3

  Out[2]= 3

  In[3]:= Join @@ ( Table[#,{n}]& /@ lst )

  Out[3]= {{A, B}, {A, B}, {A, B}, C, C, C, D, D, D}

Scott Hemphill	hemphill at
"This isn't flying.  This is falling, with style."  -- Buzz Lightyear

  • Prev by Date: Re: Re: Help Mathematica
  • Next by Date: Re: Goto Line ?
  • Previous by thread: Re: repeating elements in a list
  • Next by thread: Re: repeating elements in a list