Home > Enterprise >  Merge column into one string from 2D list
Merge column into one string from 2D list

Time:04-29

I want to merge one column into one string from 2D list. Dos there are more good way to merge it?

lists = [['H', 'W'], ['e', 'o'], ['l', 'r'], ['l', 'l'], ['o','d']]
str1 = ''
str2 = ''
for i in lists:
    str1  = i[0]
    str2  = i[1]
print(str1, str2) #('Hello', 'World')

CodePudding user response:

Another option is to use zip and str.join in a loop:

out = [''.join(tpl) for tpl in zip(*lists)]
print(*out)

Output:

Hello World
  • Related