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']