I want to exception handle 'list index out of range.'
You have two options; either handle the exception or test the length:
if len(dlist) > 1:
newlist.append(dlist[1])
continue
or
try:
newlist.append(dlist[1])
except IndexError:
pass
continue
Use the first if there often is no second item, the second if there sometimes is no second item.
A ternary will suffice. change:
gotdata = dlist[1]
to
gotdata = dlist[1] if len(dlist) > 1 else 'null'
this is a shorter way of expressing
if len(dlist) > 1:
gotdata = dlist[1]
else:
gotdata = 'null'
Handling the exception is the way to go:
try:
gotdata = dlist[1]
except IndexError:
gotdata = 'null'
Of course you could also check the len()
of dlist
; but handling the exception is more intuitive.