You need to str.split
split the strings on ","
, then you can use itertools.product
to get the cartesian product of the two lists:
from itertools import product
x = "a1,a2,a3"
y = "b1,b2,b3"
print([fst + snd for fst, snd in product(x.split(","), y.split(","))])
# ['a1b1', 'a1b2', 'a1b3', 'a2b1', 'a2b2', 'a2b3', 'a3b1', 'a3b2', 'a3b3']
You could also do this nested loops in a list comprehension to achieve the same result:
print([fst + snd for fst in x.split(",") for snd in y.split(",")])
# ['a1b1', 'a1b2', 'a1b3', 'a2b1', 'a2b2', 'a2b3', 'a3b1', 'a3b2', 'a3b3']
Or use a solution similar to your original approach:
z = []
for fst in x.split(","):
for snd in y.split(","):
z.append(fst + snd)
print(z)
# ['a1b1', 'a1b2', 'a1b3', 'a2b1', 'a2b2', 'a2b3', 'a3b1', 'a3b2', 'a3b3']