kabilan kabilan - 1 month ago 10
Python Question

Print One to many between 2 lists

Sorry for the wrong output sequence. The one that i changed now has the correct one.

I have two lists in Python as given below

a = ['a','b','c']
b = [1,2,3]


I want to print the output as

a1,b1,c1,a2,b2,c2,a3,b3,c3.

How can I achieve this?

Answer

You could simply use for loops, as below:

a = ['a','b','c']
b = [1,2,3]

for el_a in a:
    for el_b in b:
        print str(el_a) + str(el_b)

will produce : a1,a2,a3,b1,b2,b3,c1,c2,c3

[Update] For the updated sequence:

a = ['a','b','c']
b = [1,2,3]

for el_b in b:
    for el_a in a:
        print str(el_a) + str(el_b)

will produce : a1,b1,c1,a2,b2,c2,a3,b3,c3

Comments