Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine lists by joining strings with matching index values

Tags:

python

I have two lists that I would like to combine, but instead of increasing the number of items in the list, I'd actually like to join the items that have a matching index. For example:

List1 = ['A', 'B', 'C']
List2 = ['1', '2', '3']
List3 = ['A1', 'B2', 'C3']

I've seen quite a few other questions about simply combining two lists, but I'm afraid I haven't found anything that would achieve.

Any help would be much appreciated. Cheers.

like image 214
user2396930 Avatar asked May 18 '13 13:05

user2396930


Video Answer


2 Answers

>>> List1 = ['A', 'B', 'C']
>>> List2 = ['1', '2', '3']
>>> [x + y for x, y in zip(List1, List2)]
['A1', 'B2', 'C3']
like image 55
jamylak Avatar answered Oct 01 '22 09:10

jamylak


>>> List1 = ['A', 'B', 'C']
>>> List2 = ['1', '2', '3']
>>> map(lambda a, b: a + b, List1, List2)
['A1', 'B2', 'C3']
like image 35
Alexey Kachayev Avatar answered Oct 01 '22 07:10

Alexey Kachayev