Combining lists

How can I combine

list1={{x1,y1},{x2,y2},{x3,y3}}


and

list2={{x4,y4},{x5,y5}}


to

list={{x1,y1},{x2,y2},{x3,y3},{x4,y4},{x5,y5}}


?

• Harald, did you have a look at the documentation? Your question is very close to the RTFM category, so pressing F1 or googling first is usually recommended. May 6, 2012 at 12:35
• In particular, you should look at the List Manipulation section of the documentation, the tutorials near the bottom, in particular. May 6, 2012 at 13:51

You need Join :

list = Join[list1, list2]


sometimes you would choose :

listU = Union[list1, list2]


The latter doesn't include duplicates, as the first approach could, if some of elements in list1 and list2 were common.

Edit

It should be emphasized that since for small lists different approaches (pointed out in the other answers) are elegant and quite satisfactory, however for big lists Join is much superior. We compare their efficiency in a few different cases :

1. lA1 = RandomReal[1, {500000, 2}];
lA2 = RandomReal[1, {500000, 2}];

Join[lA1, lA2]; // AbsoluteTiming // First
## & @@@ {lA1, lA2}; // AbsoluteTiming // First
{lA1, lA2}~Flatten~1; // AbsoluteTiming // First

0.0210000
0.8090000
0.4620000

2. lB1 = RandomReal[1, {2500000, 2}];
lB2 = RandomReal[1, {1500000, 2}];

Join[lB1, lB2]; // AbsoluteTiming // First
## & @@@ {lB1, lB2}; // AbsoluteTiming // First
{lB1, lB2}~Flatten~1; // AbsoluteTiming // First

0.0820000
3.1500000
1.9000000

3. lC1 = RandomReal[1, {300000, 2}];
lC2 = RandomReal[1, {900000, 2}];

Join[lC1, lC2]; // AbsoluteTiming // First
## & @@@ {lC1, lC2}; // AbsoluteTiming // First
{lC1, lC2}~Flatten~1; // AbsoluteTiming // First

0.0220000
0.9320000
0.6640000


We can see that Join is roughly about 20-30 times faster than {list1, list2}~Flatten~1; and the latter is about 1.5-2 times faster than ## & @@@.

• One issue with Union is that it sorts the list, and if you don't wish to do that, other ways must be found (see Mr.Wizard's answer, in particular). May 6, 2012 at 14:36
• @rcollyer FWIW there is a slighter richer copy of that answer here. May 6, 2012 at 17:48
• @Mr.Wizard couldn't find that when I was looking for it. May 6, 2012 at 19:28
• rcollyer and Mr.Wizard Thanks for links. May 6, 2012 at 23:14

And another:

## & @@@ {list1, list2}

• Neat trick, turning each sub-list into a Sequence that is then embedded in the outer list. +1 May 6, 2012 at 15:09
• Another way to do this (by literally doing what @rcollyer described) is Sequence @@@ {list1, list2}. May 6, 2012 at 15:50
• @rcollyer One problem with this is that it will unpack both lists if they were packed. May 6, 2012 at 18:26
• @LeonidShifrin does celtschk do that, also? May 6, 2012 at 19:28
• @rcollyer Yep, he does. Anything that uses Apply on packed arrays, will unpack. May 6, 2012 at 19:32

Since there's always more than one way to do things in Mathematica, here's another alternative:

{list1, list2} ~Flatten~ 1


The above uses infix notation, which might be a little hard to grok at first, but can make the code very readable for functions that take 2 arguments and have descriptive names.

For comparison, here is the same expression written in 3 other forms:

Flatten[{list1, list2}, 1]           (* Matchfix *)
Flatten[#, 1] &@{list1, list2}       (*  Prefix  *)
{list1, list2} // Flatten[#, 1] &    (* Postfix  *)

• Yup - you might gain some infix disciples once they realize the superior readability of the very same ;-) May 6, 2012 at 15:23
• @YvesKlett I'm a worshiper at the post-fix temple; it allows me to think of each subsequent function as a sequence of transformations. It isn't necessarily the most readable; however, it is easier to conceptualize. May 6, 2012 at 15:29
• Of course consequent use of Infix syntax would lead to list1 ~List~ list2 ~Flatten~ 1 May 6, 2012 at 15:42
• @celtschk Nooooo. You don't want Mr.Wizard to read this conversation!
– rm -rf
May 6, 2012 at 15:42
• @celtschk while, yes, it can lead there, even Mr.W wouldn't do that: it requires to many extra key-strokes. And, while I think he's definitely along the s&m (sadism and masochism) spectrum as far as infix is concerned, he's not interested in wasting effort. May 6, 2012 at 16:03