Message Boards Message Boards

0
|
5211 Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Special merge of two lists

Posted 9 years ago

Hello I have these two lists:

  l1={a, b, c}
  l2={k, l, m}

How can I get

  {a, b, c, k, a, b, c, l, a, b, c, m}?

I toyed with Riffle along the line below to no avail

Thanks

Riffle[Sequence@l1, l2]
POSTED BY: wojtek potocki
6 Replies

Slight variation, Join could be faster if it matters:

Flatten[l1~Join~{#} & /@ l2]

{a, b, c, k, a, b, c, l, a, b, c, m}

POSTED BY: Vitaliy Kaurov
Posted 9 years ago

Thanks - your answer suits me. What is the advantage of the infix form compared to the normal form ie ?

        Flatten[Join[l1, {#}]] & /@ l2
POSTED BY: wojtek potocki

Hi Wojtek,

this is not very elegant, but it works:

l1 = {a, b, c}; l2 = {k, l, m};
Clear[lll];
Flatten[Thread[{lll, l2}] /. lll -> l1]

Regards -- Henrik

POSTED BY: Henrik Schachner
Posted 9 years ago
In[12]:= Append[l1, #] & /@ l2 // Flatten

Out[12]= {a, b, c, k, a, b, c, l, a, b, c, m}

This maps the pure function which appends an element to l1, onto the elements of l2, and then flattens the resulting list.

POSTED BY: David Keith
Posted 9 years ago

Special merge of two lists

POSTED BY: wojtek potocki
Posted 9 years ago

Thanks I undestood what is going on only by removing the Flatten

POSTED BY: wojtek potocki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract