12
$\begingroup$

Suppose I have the string lists {'a','b','c'} and {'1','2','3'}. How do I get Mathematica to generate the list {'a1','a2','a3','b1','b2','b3','c1','c2','c3'}?

$\endgroup$
1
  • 9
    $\begingroup$ Note that none of the lists in your question contain strings. $\endgroup$
    – Bob Hanlon
    Apr 11, 2022 at 12:36

5 Answers 5

18
$\begingroup$

You can also use Tuples:

a = {"a", "b", "c"};
n = {"1", "2", "3"};
StringJoin @@@ Tuples[{a, n}]
$\endgroup$
16
$\begingroup$
m = {"a", "b", "c"};
n = {"1", "2", "3"};

Catenate[Outer[StringJoin, m, n]]

{"a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"}

$\endgroup$
14
$\begingroup$
alist = {"a", "b", "c"};
nlist = {"1", "2", "3"};
Flatten@Outer[StringJoin, alist, nlist]

{"a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"}


For further study:

Join @@ (StringJoin @@@ Thread[Join[{#}, {nlist}]] & /@ alist)
$\endgroup$
9
$\begingroup$
Distribute[{alist,nlist},List,List,List,StringJoin]

(* {a1, a2, a3, b1, b2, b3, c1, c2, c3} *)  

where

alist = {"a", "b", "c"};
nlist = {"1", "2", "3"};
$\endgroup$
9
$\begingroup$
alist = {"a", "b", "c"};
nlist = {"1", "2", "3"};

Flatten@Table[alist[[i]] <> nlist[[j]], {j, 1, Length@nlist}, {i, 1, Length@alist}]

{"a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"}

$\endgroup$

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