Dance Party2
Dance Party2

Reputation: 7536

Python Join List Elements

Given the following lists:

A=['a','b','c']
B=[1,2]

I'd like to join the values such that each element of B is joined to each of A like this:

['a1','a2','b1','b2','c1','c2']

Thanks in advance!

Upvotes: 0

Views: 176

Answers (2)

John Coleman
John Coleman

Reputation: 51988

A 1-liner works:

[x+str(y) for x in A for y in B] 

Upvotes: 1

handle
handle

Reputation: 6299

A=['a','b','c']
B=[1,2]

C = []
for a in A:
    for b in B:
        C.append( str(a) + str(b) )
print(C)

Result:

['a1', 'a2', 'b1', 'b2', 'c1', 'c2']

Upvotes: 1

Related Questions