python logic setting variable to none

Question:

Ive done enough research to understand my logic (I believe):

I have python code to set a variable to None so that at the db level it stores the value of this variable to Null.

Logic looks like:

# when properly set to something other than `None` an example value might be: ['6.1 ', 'Medium'
thesev=cve.find("span", class_="label-warning").text
thesevcat=re.split("- ", str(thesev))

if thesevcat is None:
    thesevcat=None                    
else:
    #looking to set thesevcat='Medium' for example
    thesevcat=thesevcat[1]

sometimes thesevcat is successfully parsed the value, othertimes it cant parse it, so I want to set it to None

However, I keep getting this error:

thesevcat=thesevcat[1]
IndexError: list index out of range

what is going wrong here? Thanks

Asked By: Jshee

||

Answers:

thesevcat=thesevcat[1]
IndexError: list index out of range

List index out of range is pretty explicit, it means that if thesevcat is in fact a list (which we don’t really know seeing your code but I guess it is), it doesn’t have a 2nd index, which means it’s a list containing in maximum one element (for instance ["medium"] )

Please keep in mind that the first index of a list is 0, so if your list is your_list = ["medium"], to access "medium", you need to write your_list[0]

if you wanted to access the 2nd element of your list, then your list would need to be at least 2 element long, then you’d access it with the index 1
for instance:

your_list = ["medium", "large"]
print(your_list[1])
# would print large
Answered By: Sparkling Marcel
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.