There is no methid like get for lists but you could use itertools.islice
and next
with a default value of 0:
from itertools import islice
def get(s, ind):
return next(islice(s, ind, ind + 1), 0)
In your code using and s[ind]
will return the default 0
if the value at ind
is any falsey value like 0
, None
, False
etc.. which may not be what you want.
If you want to return a default for falsey values and to handle negative indexes you can use abs
:
def get(s, ind):
return s[ind] or 0 if len(s) > abs(ind) else 0