Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the list of strings

list = ['1', '2', '3', '4', '', '    5', '    ', '    6', '', '']

and I want to get every item after a first empty string to get the below result

list = ['    5', '    ', '    6', '', '']

note that I want to leave a empty strings that comes after

I wrote this function:

def get_message_text(list):

    for i, item in enumerate(list):

        del list[i]

        if item == '':

            break

    return list

but I am getting the wrong results for no reason I know:

['2', '4', '    5', '    ', '    6', '', '']

1 Answer

0 votes
by (36.8k points)
edited by

Just find a index of a first empty string and slice on it:

def get_message_text(lst):

    try:

        return lst[lst.index("") + 1:]

    except ValueError:  # '' is not in list

        return [] # if there's no empty string then don't return anything

 Want to gain skills in Data Science with Python? Sign up today for this Data Science with Python Course and be a master in it

Browse Categories

...