Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip a single loop iteration in python? [duplicate]

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

like image 427
cat40 Avatar asked Jun 16 '16 01:06

cat40


People also ask

How do you skip a loop twice in Python?

When you want to skip n values, call next() n+1 times (don't forget to assign the value of the last call to something) and then "call" continue.

How do you skip a specific loop in Python?

You can use a continue statement in Python to skip over part of a loop when a condition is met. Then, the rest of a loop will continue running. You use continue statements within loops, usually after an if statement.

How do you stop a for loop repeating in Python?

Python provides two keywords that terminate a loop iteration prematurely: The Python break statement immediately terminates a loop entirely. Program execution proceeds to the first statement following the loop body. The Python continue statement immediately terminates the current loop iteration.

How do you skip out of a loop?

To break out of a for loop, you can use the endloop, continue, resume, or return statement.

How to carry out iteration this for loop in Python?

To carry out the iteration this for loop describes, Python does the following: 1 Calls iter () to obtain an iterator for a 2 Calls next () repeatedly to obtain each item from the iterator in turn 3 Terminates the loop when next () raises the StopIteration exception More ...

How do you continue a while loop in Python?

The continue Statement: The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.

How do you break a loop in Python?

The break statement in Python terminates the current loop and resumes execution at the next statement, just like the traditional break found in C. The most common use for break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops. Example:

How many types of iteration are there in Python?

There are two types of iteration: Definite iteration, in which the number of repetitions is specified explicitly in advance Indefinite iteration, in which the code block executes until some condition is met In Python, indefinite iteration is performed with a while loop.


2 Answers

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
like image 195
Moses Koledoye Avatar answered Oct 12 '22 07:10

Moses Koledoye


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
like image 20
Burhan Khalid Avatar answered Oct 12 '22 08:10

Burhan Khalid