I have two lists:
list_one = ['I', 'walk','on','the','street','','','','']
list_two = ['','','','','','with','my','pajamas','on']
For both lists my objectives are different. For list_one I would like to find the last item for which the length is greater than 0. And for list_two I would like to find the index of the first variable for which the length is greater than 0.
Right now I have created two functions:
def index_last_long(sentence):
for index, word in enumerate(sentence):
if len(word) == 0:
return index-1
def index_first_long(sentence):
for index, word in enumerate(sentence):
if len(word) > 0:
return index
This does work. However, I think there must be other ways that require less code to perform the same tasks.
Who knows of such approaches?