1
$\begingroup$

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

?

$\endgroup$
2
  • 7
    $\begingroup$ 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. $\endgroup$
    – Yves Klett
    May 6 '12 at 12:35
  • $\begingroup$ In particular, you should look at the List Manipulation section of the documentation, the tutorials near the bottom, in particular. $\endgroup$
    – rcollyer
    May 6 '12 at 13:51
13
$\begingroup$

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 ## & @@@.

$\endgroup$
4
  • 2
    $\begingroup$ 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). $\endgroup$
    – rcollyer
    May 6 '12 at 14:36
  • 2
    $\begingroup$ @rcollyer FWIW there is a slighter richer copy of that answer here. $\endgroup$
    – Mr.Wizard
    May 6 '12 at 17:48
  • $\begingroup$ @Mr.Wizard couldn't find that when I was looking for it. $\endgroup$
    – rcollyer
    May 6 '12 at 19:28
  • $\begingroup$ rcollyer and Mr.Wizard Thanks for links. $\endgroup$
    – Artes
    May 6 '12 at 23:14
12
$\begingroup$

And another:

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

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  *)
$\endgroup$
9
  • 2
    $\begingroup$ Yup - you might gain some infix disciples once they realize the superior readability of the very same ;-) $\endgroup$
    – Yves Klett
    May 6 '12 at 15:23
  • 1
    $\begingroup$ @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. $\endgroup$
    – rcollyer
    May 6 '12 at 15:29
  • 2
    $\begingroup$ Of course consequent use of Infix syntax would lead to list1 ~List~ list2 ~Flatten~ 1 $\endgroup$
    – celtschk
    May 6 '12 at 15:42
  • 1
    $\begingroup$ @celtschk Nooooo. You don't want Mr.Wizard to read this conversation! $\endgroup$
    – rm -rf
    May 6 '12 at 15:42
  • 1
    $\begingroup$ @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. $\endgroup$
    – rcollyer
    May 6 '12 at 16:03

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.