Shijith
Shijith

Reputation: 4872

How to remove empty strings from the end of a list of strings (only at the end)

i have some list of strings. I want to remove empty strings from the end of the list (i.e. each list should end with a non empty element).

input
list1= ['a1','b1','c1','d1','']
list2 = ['a2','','b2','','c2','d2','']
list3 = ['a3','','b3','','','']
list4 = ['','','','','']

output

list1= ['a1','b1','c1','d1']
list2 = ['a2','','b2','','c2','d2']
list3 = ['a3','','b3']
list4 = ['']

if all the elements are empty strings , only one empty string should remain (eg. list4).

Upvotes: 2

Views: 141

Answers (6)

Reman
Reman

Reputation: 8109

list1 = ['a1','b1','c1','d1','']
list2 = ['a2','','b2','','c2','d2','']
list3 = ['a3','','b3','','','']
list4 = ['','','','','']

data = [list1, list2, list3, list4]  -->
data = [['a1','b1','c1','d1',''], ['a2','','b2','','c2','d2',''], ['a3','','b3','','',''], ['','','','','']]

for mysublist in data:
    while (mysublist[-1].rstrip() == "" and len(mysublist) > 1):
        del mysublist[-1]

For every sublist in data remove the last item if item is empty and if item is not the only* item.
Keep on doing it if there are still empty items at the end of a sublist.

(* the questioner: if all the elements are empty strings, only one empty string should remain)

Upvotes: 0

Thomas Kowalski
Thomas Kowalski

Reputation: 2184

The easiest way (in my opinion)

def remove_empty_at_end(l: list):
    i = len(l) - 1
    # If you're not sure the items of l are strings, then, you can do while l[i] == ""
    while not l[i] and i > 0: 
        i -= 1

    return l[:i + 1]

It's simple and avoids creating countless copies of l

Upvotes: 0

yatu
yatu

Reputation: 88226

You can use a generator comprehension with enumerate and keep the first index starting from the end where there is a non-empty string. By using next we only need to iterate until the first non-empty string is found:

def trim_empty_end(l):
    last_ix = next((ix for ix, i in enumerate(l[::-1]) if i), len(l)-1)
    return l[:len(l) - last_ix]

trim_empty_end(['a1','b1','c1','d1',''])
# ['a1', 'b1', 'c1', 'd1']

trim_empty_end(['a2','','b2','','c2','d2',''])
# ['a2', '', 'b2', '', 'c2', 'd2']

trim_empty_end(['a3','','b3','','',''])
# ['a3', '', 'b3']

trim_empty_end(['','','','',''])
# ['']

Upvotes: 2

Booboo
Booboo

Reputation: 43993

def trim_empty_strings(l):
    while len(l) > 1 and l[-1] == '':
        l = l[:-1]
    return l

trim_empty_strings(['a','b','', '']
trim_empty_strings(['','',''])

Upvotes: 0

Rakesh
Rakesh

Reputation: 82755

This is one approach using str methods.

Ex:

list1= ['a1','b1','c1','d1','']
list2 = ['a2','','b2','','c2','d2','']
list3 = ['a3','','b3','','','']
list4 = ['','','','','']

data = [list1, list2, list3, list4]

result = ["*#*".join(i).strip("*#* ").split("*#*") for i in data]
print(result)

Output:

[['a1', 'b1', 'c1', 'd1'],
 ['a2', '', 'b2', '', 'c2', 'd2'],
 ['a3', '', 'b3'],
 ['']]

Upvotes: 1

Tianbo Ji
Tianbo Ji

Reputation: 125

You can use recursion

def remove_empty(l):
    if l[-1] != "" or len(l) <= 1:
        return l
    return remove_empty(l[:-1])

print(remove_empty(list1)) # ['a1', 'b1', 'c1', 'd1']
print(remove_empty(list2)) # ['a2', '', 'b2', '', 'c2', 'd2']
print(remove_empty(list3)) # ['a3', '', 'b3']
print(remove_empty(list4)) # ['']

Upvotes: 0

Related Questions