I have a code like that,
i = 0
l1 = ['a','b','c']
while(i < len(l1)):
if i + 2 < len(l1):
if l1[i + 2] == 'c':
print("ok")
else:
print("None,Error!")
else:
print("None,Error!")
i += 1
As you can see, in the else
part, print("None,Error!")
are used in two times. In practice, they are very long but totally same so I want to merge them to make them simple.However, I need to check if i+2
is out of list bound, so I cannot rewrite it to
if i+2 < len(l1) and l1[i + 2] == 'c':
Is there any idea can solve this problem?