Nicholas
Nicholas

Reputation: 1

How do I get python to take element 0 and 1 if they exist and take element 0 if element 1 does not exist?

I'm getting "Index Error:List index out of range error" when I try to run my code. Essentially, I want to identify all the times "Item 1A" is referenced in my documents and keep that section. Unfortunately, some of the documents have a Table of Contents (TOC) and some do not. If there is a TOC, then the element I want is element 1, when there is no table of contents, I want element 0. I figured the easiest was just to keep both element 0 and 1 because it won't hurt to have one line from the TOC in there. Unfortunately, my code won't run because in the instances where there is not a TOC, the element I am calling on (element 1) is out of range. Any idea how to get it to take elements 0 and 1 whether or not they exist? Here's my code. Thanks!

#Using the period will identify the header; references shouldn't have a period following
docm=re.split('Item 1A\.',doc, re.IGNORECASE)
#Element 0 of list should be the table of contents entry
docm=re.findall('Item 1A\..*?Item',doc, re.IGNORECASE)

#Identifies the next item after 1a (i.e. 1b or 2)
docs=re.sub(' |\.','',doc.split(docm[0])[1][:4]) 
#Finds all text between item 1a. and the next item, element 0 is table of contents, element 1 is what we want
docss=re.findall('item 1a\..*?item '+docs+'\.',doc,re.IGNORECASE)[0-1]

Upvotes: 0

Views: 62

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798744

Slice out both and take the last item.

seq[0:2][-1]

Upvotes: 1

Related Questions