7

I have the following code:

for i in list1:
    if i == 5:
        #skip the NEXT iteration (not the end of this one)
    else:
        #do something

How do I skip the iteration that comes after the iteration that throws the skip. For example, if list1=[1, 2, 3, 4, 5, 6, 7], the loop will skip 6 and go straight to 7 because 5 triggered the skip I've seen this question and several others, but they all deal with skipping the current iteration, while I want to skip the next iteration. The answers to those questions suggest continue, which as far as I can tell will stop the remainder of the current iteration and move on to the next one, which is not what I want. How can I skip a single iteration in a loop?

Edit: Using next() has been suggested, but this does not work for me. When I run the following code:

a = [1, 2, 3, 4, 5, 6, 7, 8]
ai = iter(a)
for i in a:
    print i
    if i == 5:
        _ = next(ai)

I get

1
2
3
4
5
6 #this should not be here
7
8

Using next() is also the suggestion in this question: Skip multiple iterations in loop python

1
  • 1
    There's your bug: for i in a:. Should be for i in ai: Commented Jun 16, 2016 at 16:06

3 Answers 3

16

You can create an iterator from the list. With this, you can mutate the loop items while looping:

it = iter(list1)
for i in it:
    if i == 5:
        next(it) # Does nothing, skips next item
    else:
        #do something 

In case you're planning to use the value at i==5, you should do something before the evaluating the condition:

it = iter(list1)
for i in it:
    #do something
    if i == 5:
        next(it) # Does nothing, skips next item

If you're doing this in a terminal, you should assign the next item to a variable as the terminal may force an autoprint on a dangling reference:

>>> list1 = [1, 2, 3, 4, 5, 6, 7]
>>> it = iter(list1)
>>> for i in it:
...    print(i)
...    if i == 5:
...       j = next(it) # here
...
1
2
3
4
5
7
Sign up to request clarification or add additional context in comments.

5 Comments

This doesn't seem to actually skip the next iteration. When I add print i to the first line of the loop, it prints 1 though 7, when it should print 1, 2, 3, 4, 5, 7, skipping 6
I'm not using the terminal. I wrote and saved (and ran) a .py file
And it still does not work after you passed the output to a variable?
Yes, it still does not work
Please update your question with the changes you have made. or you could post the code in the comments if you choose not to, so we can have a look.
5

Just set a flag:

>>> i
[0, 1, 2, 3, 4, 5, 6, 7]
>>> skip = False
>>> for q in i:
...    if skip:
...      skip = False
...      continue
...    if q == 5:
...       skip = True
...    print(q)
...
0
1
2
3
4
5
7

Comments

0
runNext = True
for i in list1:
    if i == 5:
        runNext = False
    else:
        if runNext:
            #do something
        runNext = False

1 Comment

That's what I'm already doing. Is there a better method?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.