user3030473 user3030473 - 4 months ago 9
Python Question

Python append to an string element in a list

I'm fairly new to python. I want to know how can you append to a string element in a list cumulatively ?

list = ['1','2','3','4']
list2 = ['a','b','c','d']


I want a new list like this:

list3 = ['1a', '1b', '1c', '1d']


I've been running circles for possible answers. Help is much appreciated, thanks !

Answer

You can use map() and zip like so:

list = ['1','2','3','4']
list2 = ['a','b','c','d'] 

print map(lambda x: x[0] + x[1],zip(list,list2))

Output:

['1a', '2b', '3c', '4d']

Online Demo - https://repl.it/CaTY