5
$\begingroup$

This is something that should be simple, but the documentation and other questions don't seem to deal with it. I can think of mechanically doing it through a loop, but I know there must be a better way.

In this example, I have three lists (although the actual operation will involve several sets of 20-30 each, and they're much longer):

list1={1,5,5,5,5,6};
list2={2,1,2,2,2,2};
list3={3,3,3,3,3,3};

What I need to do is add them together - as in all the first positions of the three lists being added together, all the second positions, and all the third positions, so that I just have say:

list4={6,9,10,10,10,11};

The actual order of the list is important (I'm using it to store data). Thanks in advance.

$\endgroup$

3 Answers 3

18
$\begingroup$

There is another approach (much better) - Total :

Total[{list1, list2, list3}]
{6, 9, 10, 10, 10, 11}

or Apply the function Plus :

Plus @@ {list1, list2, list3}

or

MapThread[ Plus, {list1, list2, list3}]
$\endgroup$
14
  • $\begingroup$ Interesting. Total and Plus @@ are about 50% faster than list1 + list2 + list3 (they are the same speed as each other). I wonder why. The truly odd thing is that MapThread is more than two orders of magnitude slower! $\endgroup$
    – Mark Adler
    Commented Jul 20, 2012 at 15:08
  • 3
    $\begingroup$ @MarkAdler Are you sure you aren't micro-optimizing and looking at negligible differences on tiny lists? I can't imagine Plus@@ being faster, as Apply unpacks the list... $\endgroup$
    – rm -rf
    Commented Jul 20, 2012 at 15:11
  • $\begingroup$ @MarkAdler It depends on the data you are deal with, in general Total is faster than other approaches, nevertheless if we deal with unpacked lists, Plus would be faster. $\endgroup$
    – Artes
    Commented Jul 20, 2012 at 15:14
  • $\begingroup$ @Artes, I prefer your answer to mine. $\endgroup$
    – yulinlinyu
    Commented Jul 20, 2012 at 15:23
  • 1
    $\begingroup$ @MarkAdler It is in fact better if you test more extensively varoius types of lists, not restricting to this problem literally. You can use it in more general situations, then it apears to be better (not always). Look at the documentation center to read Total and try more examples to compare with Plus. Consider also comparing it with Plus@@@list[list1,list2,...,listn]. $\endgroup$
    – Artes
    Commented Jul 21, 2012 at 6:30
19
$\begingroup$

Do you mean list4 = list1 + list2 + list3 ?

(* {6, 9, 10, 10, 10, 11} *)
$\endgroup$
3
  • $\begingroup$ Oh my god. This is embarrassing. I've been reading through the List documentation for ages, when this was right in front of me. $\endgroup$ Commented Jul 20, 2012 at 14:32
  • $\begingroup$ I'm happy to close as long as you get your rep.. $\endgroup$ Commented Jul 20, 2012 at 14:33
  • 4
    $\begingroup$ @ian.milligan It might be embarrassing, but it's not a bad question and it may help future visitors. $\endgroup$ Commented Jul 20, 2012 at 15:22
6
$\begingroup$

This is complementary to the other answers — since you say that you have 20-30 lists that need to be added, you can't be entering them all by hand. Here's a way to build up the lists you want to add:

ToExpression@StringJoin @@ Riffle["list" <> ToString@# & /@ Range[10], "+"]
(* list1+list2+list3+list4+list5+list6+list7+list8+list9+list10 *)
$\endgroup$
2
  • 6
    $\begingroup$ I think that if there are 20-30 lists, then storing them in separate variables (not an indexed variable or array) is a plain bad idea to start with, which I would not encourage. But apart from that, using string manipulations I view as a last resort, since they are error-prone. It is always better to work with already parsed code, and if you can't, it usually indicates (to me at least) that something is wrong with the data representation. $\endgroup$ Commented Jul 20, 2012 at 14:50
  • 1
    $\begingroup$ @LeonidShifrin I agree and I wouldn't do it this way, but apparently it looks like the OP has things set up this way, hence the question... $\endgroup$
    – rm -rf
    Commented Jul 20, 2012 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.