2

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

2
  • 2
    Should be pretty simple - any attempt from you? Commented May 28, 2019 at 12:37
  • hi , cannot upvote , showing -1, while trying to upcode, don't know why Commented May 28, 2019 at 12:53

6 Answers 6

2

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(['','','','',''])
# ['']
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the reply, it will fail for the last case list4.
Added a default value to the next method. Works now @Shijith
1

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

5 Comments

what's with the downvotes here...? someone just sistematically downvoted all answers
Wrong when list5 = ['a1','b1','c1','*',''], it returns ['a1','b1','c1'].
@yatu.yes.. I just saw that
@TianboJi. Probably OP has to confirm this.And also its pretty easy to create your own unique identifier....I just used *#* as an example
@rakesh, yes, for ['a1','b1','c1','*',''], it returns ['a1','b1','c1']', but for me this case will not arise since i am removing * and #` for the list using a regex , before this step. this also fails for #
0

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

1 Comment

Using recursion in Python in answer to such a simple problem seems like a bad idea.
0
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(['','',''])

Comments

0

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

1 Comment

Does not work: remove_empty_at_end(['a', 'b', ''] returns ['a']
0
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)

2 Comments

Please add description about how the code works so that the questioner can understand the solution.
@Pardhu added description

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.