Assume something like this:
if mylist[0] == 1 or mylist[12] == 2:
# do something
But I’m not sure if mylist[12] will always not be out of range. What do to keep things simple and still check if index exists? Wouldn’t want to do
if mylist[0] == 1:
# do something
elif mylist[12] == 2:
# do the EXACT same thing
As you get too much identical lines of code.
You could check the length of the list:
This uses the short-circuting behaviour of
andto ensure thatmylist[12]won’t be evaluated if the list has 12 items or fewer.