# How can I generate a list of combinations of string joins?

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

• Note that none of the lists in your question contain strings. Commented Apr 11, 2022 at 12:36

You can also use Tuples:

a = {"a", "b", "c"};
n = {"1", "2", "3"};
StringJoin @@@ Tuples[{a, n}]

m = {"a", "b", "c"};
n = {"1", "2", "3"};

Catenate[Outer[StringJoin, m, n]]


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

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)

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

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

a = {"a", "b", "c"};

b = {"1", "2", "3"};


Using Function and StringJoin

(n |-> a[[n]] <> # & /@ b) /@ Range[Length @ b]


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