Element wise concatenate multiple lists (list of list of strings)
Here's one way zipping the sublists and mapping with ''.join
the resulting tuples:
list(map(''.join, zip(*lst)))
# ['a@1', 'b$2', 'c#3']
Here zip
as shown in the docs aggregates elements from several iterables. With *
, we are unpacking the list into separate iterables, which means that the function will instead be receiving zip(['a','b','c'],['@','$','#'],['1','2','3'])
.
Now at each iteration, map
will be applying ''.join
to each of the aggregated iterables, i.e to the first element in each sublist, then the second, and so on.
Not as elegant as yatu's answer but if you're using pandas:
import pandas as pd
pd.DataFrame(lst).sum(axis=0)
# 0 a@1
# 1 b$2
# 2 c#3
# dtype: object
Pandas Series have a .tolist()
method to get the expected output:
series = pd.DataFrame(lst).sum(axis=0)
series.tolist()
# ['a@1', 'b$2', 'c#3']
I had the following issues also with strings inside the list,
1) extra white spaces including leading and trailing,
2) also had to concatenate the strings with space in between.
3) Length of list inside the list are not the same always, for the below example length of list[0]
,list[1]
and list[2]
are 4,4 and 3
.
example list as below :
lst= [[' It is raining ',' Hello Mr. x',' Life is what happens','This'],
['cats and dogs','how are you','when you are busy','ends here'],
['here now ','doing today? ','making other plans ']]
First and second cases can be solved by spliting each element in the list (default split using space, which will remove all the white spaces) and then joining them together with a single space in between.
Concatenating elements of varying length can be done using itertools.zip_longest
, with fillvalue=''
, as below:
from itertools import zip_longest
[' '.join(x.split()) for x in map(' '.join, zip_longest(*lst,fillvalue=''))]
output as below:
['It is raining cats and dogs here now',
'Hello Mr. x how are you doing today?',
'Life is what happens when you are busy making other plans',
'This ends here']