12
$\begingroup$

My attempt:

data = {{a, 3}, {b, 5}, {c, 2}};

output = Table[#[[1]], #[[2]]] & /@ data // Flatten

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

$\endgroup$
  • 2
    $\begingroup$ Also Splice@*ConstantArray@@@data $\endgroup$ – user1066 Jun 24 at 10:52
  • 3
    $\begingroup$ Benchmark: Join @@ ConstantArray @@@ data < (Faster than) Flatten[ConstantArray @@@ data] < Splice@*ConstantArray @@@ data < Join @@ Table @@@ data < Flatten[Table @@@ data]. $\endgroup$ – SneezeFor16Min Jun 24 at 14:42
20
$\begingroup$
Join @@ Table @@@ data
{a, a, a, b, b, b, b, b, c, c}

or

Join @@ ConstantArray @@@ data
{a, a, a, b, b, b, b, b, c, c}
## & @@ PadRight[{}, #2, {#}] & @@@ data
{a, a, a, b, b, b, b, b, c, c}
## & @@ PadRight[{#}, #2, "Fixed"] & @@@ data
{a, a, a, b, b, b, b, b, c, c}
| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ I have to wait for 11 minutes to accept. $\endgroup$ – Too Fat Man No Neck Jun 24 at 1:13
  • 7
    $\begingroup$ Also Flatten[Table @@@ data] and Flatten[ConstantArray @@@ data] $\endgroup$ – Bob Hanlon Jun 24 at 2:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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