Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python logic setting variable to none

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# 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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading