python - Joining elements of a list if those elements are in between two whitespaces




slice (5)

I have an input like this:

['assembly', '', 'py', 'tho', 'n', '', 'ja', 'va', '', 'rub', 'y', '', 'java', 'script', '', 'c++']

I want to join elements between '' to have an output like this:

['assembly', 'python', 'java', 'ruby', 'javascript', 'c++']

I tried using join and list slicing like this:

a=['assembly', '', 'py', 'tho', 'n', '', 'ja', 'va', '', 'rub', 'y', '', 'java', 'script', '', 'c++']
a[2:5] = [''.join(a[ 2: 5])]
a=['assembly', '', 'python', '', 'ja', 'va', '', 'rub', 'y', '', 'java', 'script', '', 'c++']

This works to some extent but I don't know how to iterate this instruction for the entire list.


If the input delimiters are actually empty strings, then you can do

strlist = [x or ' ' for x in a]
joined = ''.join(strlist).split()
joined
['assembly', 'python', 'java', 'ruby', 'javascript', 'c++']

If you can't or don't want to use itertools:

l = ['assembly', '', 'py', 'tho', 'n', '', 'ja', 'va', '', 'rub', 'y', '', 'java', 'script', '', 'c++']
l_new = []
combined = ""
for idx, s in enumerate(l):
    if s != "":
        combined += s
        if idx == len(l)-1:
            l_new.append(combined)

    else:
        l_new.append(combined)
        combined = ""

This is awful and hacky, but

lambda b:lambda l:''.join(i or b for i in l).split(b)

can take any string you can guarantee is not contained in the concatenation of the list, and return a function doing what you want. Of course, you probably want to only use this once or twice for your specific situation, so, if you can guarantee that no element of the list contains a space, it might look more like:

a = ['assembly', '', 'py', 'tho', 'n', '', 'ja', 'va', '', 'rub', 'y', '', 'java', 'script', '', 'c++']
a = ''.join(i or ' ' for i in a).split(' ')

Using itertools.groupby :

from itertools import groupby

l = ['assembly', '', 'py', 'tho', 'n', '', 'ja', 'va', '', 'rub', 'y', '', 'java', 'script', '', 'c++']
new_l = [''.join(g) for k, g in groupby(l, key = bool) if k]

Output:

['assembly', 'python', 'java', 'ruby', 'javascript', 'c++']

run a loop over the list
inside the loop append the element to a temporary empty string and check for the condition whether the element is an empty string or last element of the list, if true then append the temporary variable to output list and change the value of that variable to a empty string
Code:

x=['assembly', '', 'py', 'tho', 'n', '', 'ja', 'va', '', 'rub', 'y', '', 'java', 'script', '', 'c++']
temp=''
output=[]
for y in x:
    temp=temp+y
    if y=='' or y==x[-1]:
        output.append(temp)
        temp=''

print(output)

Output: ['assembly', 'python', 'java', 'ruby', 'javascript', 'c++']





whitespace