简体   繁体   中英

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 (ie 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).

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(['','','','',''])
# ['']

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

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)) # ['']

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

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(['','',''])
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM