python "or" when one of the vars might not exist?
if-statement, list, python
Solution
You could check the length of the list:
if mylist[0] == 1 or (len(mylist) > 12 and mylist[12] == 2):
This uses the short-circuting behaviour of `and` to ensure that `mylist[12]` won't be evaluated if the list has 12 items or fewer.
Problem
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.