liotur
liotur

Reputation: 923

Concatenate zipped values in Python

I am just starting learn python. Please advise how I can concatenate this.

a='abc'
b='123'

As output I want:

[(a1, b2, c3)]

I tried to use built in zip() function but result is (a, 1), (a, 2), (a, 3)

Upvotes: 2

Views: 5836

Answers (3)

Marcus.Aurelianus
Marcus.Aurelianus

Reputation: 1518

Because you can directly addup two string together, therefore you can try:

[x+y for x,y in zip(a,b)]

Upvotes: 4

Sunitha
Sunitha

Reputation: 12015

You can use ''.join and map. Would come handy when you have to join more that 2 elements

>>> list(map(''.join, zip(a,b)))
['a1', 'b2', 'c3']

Upvotes: 0

Andrej Kesely
Andrej Kesely

Reputation: 195438

You need to concatenate the values after zip:

a='abc'
b='123'

print([v1 + v2 for v1, v2 in zip(a, b)])

Prints:

['a1', 'b2', 'c3']

Upvotes: 1

Related Questions