1

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

?

rm -rf
  • 88,781
  • 21
  • 293
  • 472
Harald
  • 1,573
  • 1
  • 11
  • 15

3 Answers3

14

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

Artes
  • 57,212
  • 12
  • 157
  • 245
13

And another:

## & @@@ {list1, list2}
user1066
  • 17,923
  • 3
  • 31
  • 49
10

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  *)
rm -rf
  • 88,781
  • 21
  • 293
  • 472
  • Why not add the other equivalent notations as well to show their relations? – Yves Klett May 06 '12 at 15:20
  • @YvesKlett You mean prefix and postfix and matchfix? – rm -rf May 06 '12 at 15:20
  • 2
    Yup - you might gain some infix disciples once they realize the superior readability of the very same ;-) – Yves Klett May 06 '12 at 15:23
  • 1
    @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. – rcollyer May 06 '12 at 15:29
  • 2
    Of course consequent use of Infix syntax would lead to list1 ~List~ list2 ~Flatten~ 1 – celtschk May 06 '12 at 15:42
  • 1
    @celtschk Nooooo. You don't want Mr.Wizard to read this conversation! – rm -rf May 06 '12 at 15:42
  • @R.M you brought that one unto yourself [donning flak gear]. – Yves Klett May 06 '12 at 15:49
  • 1
    @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. – rcollyer May 06 '12 at 16:03
  • If y'all are trying to amuse me you're doing a good job. :^) – Mr.Wizard May 06 '12 at 17:51