Depending on the real need, the one or the other of the following code snippets may be preferable.
(1) Arrange the range.
R = [0..2] + [20] + [5..8]
for r in R:
print r
(2) Define a function that does the right thing for the right k
:
def f(k):
if k == 3: print 20
elif k == 4: pass
else: print k
for k in [0..8]:
f(k)
(3) Do the same as above without function.
for k in [0..8]:
if k == 4: continue
elif k == 3: print 20
else: print k
(4) If in the real application we have no good control of the position, after that we skip, we can use a flag, skipNext
say...
def myCondition(k):
if k == 3:
return True
return False
nextSkip = False
for k in range(9):
oldSkip = nextSkip
nextSkip = myCondition(k)
if oldSkip:
continue
if nextSkip:
print 20
else:
print k
You see how hard is to guess the "needed solution".
The question is a pure python question, consider reviewing data structures in python and logical ramification possibilites.