How to get items after something in a list in python?

Viewed 7165

I am using textblob to parse the text in english. Here is a list of words.

['Get', 'coffee', 'and', 'water', 'here']
  1. I want to get all the words after the item 'Get' in a new list called list1.

  2. I want to get all the words between 'Get' and 'here' in a new list called list2.

What would be the best way to do that? Are there any prebuilt functions for doing it as I want to keep my script clean and simple. Any suggestions will be highly appreciated.

I tried using these functions but they are not working, returning any empty list.

def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""

def find_between_r( s, first, last ):
    try:
        start = s.rindex( first ) + len( first )
        end = s.rindex( last, start )
        return s[start:end]
    except ValueError:
        return ""
2 Answers
Related