I have a list {a,b,c,d} and a second list where is how many times the elements of the first list repeat {1,4,2,6} (the order is according with the first list). Then I need to create a third list with this information: {a,b,b,b,b,c,c,d,d,d,d,d,d}. What can I do?


marked as duplicate by Kuba list-manipulation Mar 22 '18 at 7:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Catenate @ MapThread[ConstantArray, {{a, b, c, d}, {1, 4, 2, 6}}]

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

  • $\begingroup$ I'm leaving my answer but please vote for this instead (ht kglr). $\endgroup$ – Alan Mar 22 '18 at 2:29
  • 1
    $\begingroup$ Catenate not needed. Flatten@MapThread[ConstantArray, {{a, b, c, d}, {1, 4, 2, 6}}] works. Has advantage of working in older versions without Catenate. $\endgroup$ – m_goldberg Mar 22 '18 at 3:06

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