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.

-

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

-
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! – Mark Adler Jul 20 '12 at 15:08
@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... – R. M. Jul 20 '12 at 15:11
@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. – Artes Jul 20 '12 at 15:14
@Artes, I prefer your answer to mine. – yulinlinyu Jul 20 '12 at 15:23
@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]. – Artes Jul 21 '12 at 6:30

Do you mean list4 = list1 + list2 + list3 ?

(* {6, 9, 10, 10, 10, 11} *)

-
Oh my god. This is embarrassing. I've been reading through the List documentation for ages, when this was right in front of me. – programming_historian Jul 20 '12 at 14:32
I'm happy to close as long as you get your rep.. – programming_historian Jul 20 '12 at 14:33
@ian.milligan It might be embarrassing, but it's not a bad question and it may help future visitors. – Brett Champion Jul 20 '12 at 15:22

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 *)

-
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. – Leonid Shifrin Jul 20 '12 at 14:50
@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... – R. M. Jul 20 '12 at 15:10