thecheech
thecheech

Reputation: 2211

How do I make a list concatenating the corresponding elements of two lists of strings in Python?

I want to make a new list concatenating the corresponding elements of lists of equal-sized strings. For instance, for

list1 = ["a","b","c"]
list2 = ["d","e","f"]

, the output should be

["ad", "be", "cf"]

Upvotes: 0

Views: 275

Answers (4)

yejinxin
yejinxin

Reputation: 596

you can also use join instead of +

print [''.join(x) for x in zip(listone, listtwo)]

Upvotes: 1

gravetii
gravetii

Reputation: 9624

You can use list comprehension and the zip() method-

print [m + n for m, n in zip(listone, listtwo)]

Upvotes: 2

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

Use map:

>>> from operator import add
>>> one = ["a", "b", "c"]
>>> two = ["d", "e", "f"]
>>> map(add, one, two)
['ad', 'be', 'cf']

Upvotes: 4

sshashank124
sshashank124

Reputation: 32189

Firstly, your chars should be in single/double quotes:

listone = ['a', 'b', 'c']
listtwo = ['d', 'e', 'f']

Then you can do:

listthree = [i+j for i,j in zip(listone,listtwo)]

>>> print listthree
['ad', 'be', 'cf']

Upvotes: 3

Related Questions