Message Boards Message Boards

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

Special merge of two lists

Posted 10 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
Posted 10 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
Posted 10 years ago

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

POSTED BY: wojtek potocki
Posted 10 years ago

Special merge of two lists

POSTED BY: wojtek potocki

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

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 10 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
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