2
$\begingroup$

I have two list to begin with.

list1 = {1, 2, 3};
list2 = {10, 20, 30, 40};

I want to obtain the following output (order unimportant):

{11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43}

I can obtain it via "Table, For, Do" etc. command but I am looking for a concise code to achieve such an output?

$\endgroup$
4
$\begingroup$
Outer[Plus, list2, list1] // Flatten

enter image description here

$\endgroup$
4
$\begingroup$

A variation of Coolwater's answer:

Tuples @ Unevaluated[{10, 20, 30, 40} + {1,2,3}]

{11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43}

$\endgroup$
2
$\begingroup$
list1+#&/@list2//Flatten

(*{11,12,13,21,22,23,31,32,33,41,42,43}*)
$\endgroup$
2
$\begingroup$
Total[Tuples[{list2, list1}], {2}]
(* {11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43} *)
$\endgroup$

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.