Combining two string lists

  • A+

Looking to combine two list variables containing string elements. For example, if I have the below :

mystring1 = ['Luke', 'Han'] mystring2 = ['Skywalker', 'Solo'] 

I am looking to combine them to be :

mystring3 = ['LukeSkywalker', 'HanSolo'] 

I am sure I am missing something simple. Any guidance would be great!


Simply map the two lists to str.__add__:

list(map(str.__add__, mystring1, mystring2)) 

This outputs:

['LukeSkywalker', 'HanSolo'] 

As a side note, if you want to combine more than two lists of strings at a time, you can do:

mystring1 = ['Luke', 'Han'] mystring2 = ['Skywalker', 'Solo'] mystring3 = ['Hero', 'Sidekick'] from functools import partial, reduce print(list(reduce(partial(map, str.__add__), (mystring1, mystring2, mystring3)))) 

This outputs:

['LukeSkywalkerHero', 'HanSoloSidekick'] 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: