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"]
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"]
Use map
:
>>> from operator import add
>>> one = ["a", "b", "c"]
>>> two = ["d", "e", "f"]
>>> map(add, one, two)
['ad', 'be', 'cf']
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']
You can use list comprehension and the zip()
method-
print [m + n for m, n in zip(listone, listtwo)]
you can also use join
instead of +
print [''.join(x) for x in zip(listone, listtwo)]