for uses iter(song) to loop; you can do this in your own code and then advance the iterator inside the loop; calling iter() on the iterable again will only return the same iterable object so you can advance the iterable inside the loop with for following right along in the next iteration.

Advance the iterator with the next() function; it works correctly in both Python 2 and 3 without having to adjust syntax:

song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
song_iter = iter(song)
for sing in song_iter:
    print sing
    if sing == 'look':
        next(song_iter)
        next(song_iter)
        next(song_iter)
        print 'a' + next(song_iter)

By moving the print sing line up we can avoid repeating ourselves too.

Using next() this way can raise a StopIteration exception, if the iterable is out of values.

You could catch that exception, but it'd be easier to give next() a second argument, a default value to ignore the exception and return the default instead:

song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
song_iter = iter(song)
for sing in song_iter:
    print sing
    if sing == 'look':
        next(song_iter, None)
        next(song_iter, None)
        next(song_iter, None)
        print 'a' + next(song_iter, '')

I'd use itertools.islice() to skip 3 elements instead; saves repeated next() calls:

from itertools import islice

song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
song_iter = iter(song)
for sing in song_iter:
    print sing
    if sing == 'look':
        print 'a' + next(islice(song_iter, 3, 4), '')

The islice(song_iter, 3, 4) iterable will skip 3 elements, then return the 4th, then be done. Calling next() on that object thus retrieves the 4th element from song_iter().

Demo:

>>> from itertools import islice
>>> song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
>>> song_iter = iter(song)
>>> for sing in song_iter:
...     print sing
...     if sing == 'look':
...         print 'a' + next(islice(song_iter, 3, 4), '')
... 
always
look
aside
of
life
Answer from Martijn Pieters on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ skip for loop steps in python like in c++
r/learnprogramming on Reddit: Skip for loop steps in Python like in C++
April 18, 2023 -

Hi all,

I am trying to skip values with an if statement in a for loop in Python and I cannot seem to do it.

What I am trying to do in Python is the following in C++:

[in]:

#include <iostream>

using namespace std;

int main()

{

for (int i = 0;i < 10;i++)

{if (i == 3) i++;

cout<<i;}

}

[out]: 012456789.

When I do this in Python, the for loop does not "skip" value 3, it just prints 4 twice instead of 3 and 4.

I can take this to an extreme and say if i=3: i == 20 and weirdly Python distinguishes between the i inside the for loop and the i with the given value 20 which baffles me.

[in]:

for i in range(10):if i == 3: i = i+1 print(i)

[out] :0 1 2 4 4 5 6 7 8 9

Is there any solution for this?

Thank you in advance for the help!

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74334349 โ€บ how-to-skip-the-next-two-iterations-in-for-loop-using-python
How to skip the next two iterations in for loop using python? - Stack Overflow
arr = [1,2,3,4,5,6,7,8,9,10] skip_count = 2 # how many to skip counter = skip_count for num in arr: if counter == skip_count: counter = 0 print(num) else: counter += 1
Discussions

Python skip next index in loop without next(iterator) possible?
Iโ€™m writing a program i Python where I want a for loop to skip the next index in a list if certain conditions are met, I do however want to access the current and next item to do some computations in these cases i.e. myLโ€ฆ More on forum.level1techs.com
๐ŸŒ forum.level1techs.com
0
0
May 5, 2019
python - Skip multiple iterations in loop - Stack Overflow
1 How do I use something like next() in Python, but it allows me to "step" over iterations like in a for loop? -2 Can you skip the next iteration of a for loop in python? More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How do I skip a few iterations in a for loop - Stack Overflow
More specifically, I want something ... it would skip the whole loop and increase the counter by 10. If I were using a for loop in C I'd just sum 10 to i, but in Python that doesn't really work. ... You cannot alter the target list (i in this case) of a for loop. Use a while loop instead: ... from itertools import islice numbers = iter(range(10)) for i in numbers: if i == 2: ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How can you skip ahead in a for loop?
I think you are describing the continue keyword. More on reddit.com
๐ŸŒ r/learnpython
16
2
July 21, 2017
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ skip-iterations-in-for-loop-python
How to skip Iterations in For Loop - Python - GeeksforGeeks
July 23, 2025 - In a for loop, you can use continue to skip a specific iteration when a condition is true. When Python sees continue, it skips the rest of the current iteration and moves to the next iteration.
๐ŸŒ
Level1Techs
forum.level1techs.com โ€บ dev zone
Python skip next index in loop without next(iterator) possible? - Dev Zone - Level1Techs Forums
May 5, 2019 - Iโ€™m writing a program i Python where I want a for loop to skip the next index in a list if certain conditions are met, I do however want to access the current and next item to do some computations in these cases i.e. myLโ€ฆ
Top answer
1 of 7
73

for uses iter(song) to loop; you can do this in your own code and then advance the iterator inside the loop; calling iter() on the iterable again will only return the same iterable object so you can advance the iterable inside the loop with for following right along in the next iteration.

Advance the iterator with the next() function; it works correctly in both Python 2 and 3 without having to adjust syntax:

song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
song_iter = iter(song)
for sing in song_iter:
    print sing
    if sing == 'look':
        next(song_iter)
        next(song_iter)
        next(song_iter)
        print 'a' + next(song_iter)

By moving the print sing line up we can avoid repeating ourselves too.

Using next() this way can raise a StopIteration exception, if the iterable is out of values.

You could catch that exception, but it'd be easier to give next() a second argument, a default value to ignore the exception and return the default instead:

song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
song_iter = iter(song)
for sing in song_iter:
    print sing
    if sing == 'look':
        next(song_iter, None)
        next(song_iter, None)
        next(song_iter, None)
        print 'a' + next(song_iter, '')

I'd use itertools.islice() to skip 3 elements instead; saves repeated next() calls:

from itertools import islice

song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
song_iter = iter(song)
for sing in song_iter:
    print sing
    if sing == 'look':
        print 'a' + next(islice(song_iter, 3, 4), '')

The islice(song_iter, 3, 4) iterable will skip 3 elements, then return the 4th, then be done. Calling next() on that object thus retrieves the 4th element from song_iter().

Demo:

>>> from itertools import islice
>>> song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
>>> song_iter = iter(song)
>>> for sing in song_iter:
...     print sing
...     if sing == 'look':
...         print 'a' + next(islice(song_iter, 3, 4), '')
... 
always
look
aside
of
life
2 of 7
10
>>> song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
>>> count = 0
>>> while count < (len(song)):
    if song[count] == "look" :
        print song[count]
        count += 4
        song[count] = 'a' + song[count]
        continue
    print song[count]
    count += 1

Output:

always
look
aside
of
life
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-break-and-python-continue-how-to-skip-to-the-next-function
Python Break and Python Continue โ€“ How to Skip to the Next Function
March 14, 2022 - If you ever need to skip part of the current loop you are in or break out of the loop completely, then you can use the break and continue statements. In this article, I will cover how to use the break and continue statements in your Python code.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ how to skip iterations in a python for loop
How to Skip Iterations in a Python For Loop - Spark By {Examples}
May 31, 2024 - We can skip the for loop iteration using continue statement in Python. For loop iterates blocks of code until the condition is False. Sometimes it would
Find elsewhere
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ skip iterations in python loop
Skip Iterations in Python loop [2 ways] - Java2Blog
November 29, 2022 - The following code uses the continue statement to skip iterations in a Python loop. ... As we see from the above code that when the number 2 is encountered, the iteration is skipped and we skip to the next iteration without disrupting the flow of the code.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How to Break Out of Multiple Loops in Python | DigitalOcean
August 7, 2025 - When you run this code, you will ... causes a program to break out of a loop. The continue statement allows you to skip over the part of a loop where an external condition is triggered, but to go on to complete the rest of the loop....
Top answer
1 of 6
40

You cannot alter the target list (i in this case) of a for loop. Use a while loop instead:

while i < 10:
    i += 1
    if i == 2:
        i += 3

Alternatively, use an iterable and increment that:

from itertools import islice

numbers = iter(range(10))
for i in numbers:
    if i == 2:
        next(islice(numbers, 3, 3), None)  # consume 3

By assigning the result of iter() to a local variable, we can advance the loop sequence inside the loop using standard iteration tools (next(), or here, a shortened version of the itertools consume recipe). for normally calls iter() for us when looping over a iterator.

2 of 6
27

The best way is to assign the iterator a name - it is common have an iterable as opposed to an iterator (the difference being an iterable - for example a list - starts from the beginning each time you iterate over it). In this case, just use the iter() built-in function:

numbers = iter(range(100))

Then you can advance it inside the loop using the name. The best way to do this is with the itertools consume() recipe - as it is fast (it uses itertools functions to ensure the iteration happens in low-level code, making the process of consuming the values very fast, and avoids using up memory by storing the consumed values):

from itertools import islice
import collections

def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

By doing this, you can do something like:

numbers = iter(range(100))
for i in numbers: 
    ...
    if some_check(i):
        consume(numbers, 3)  # Skip 3 ahead.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-skip-specific-iterations-in-loops-419937
How to skip specific iterations in loops | LabEx
The continue statement is a powerful tool in Python loops that allows you to skip the current iteration and move to the next one without terminating the entire loop. for item in iterable: if condition: continue ## Code to execute when condition ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how can you skip ahead in a for loop?
r/learnpython on Reddit: How can you skip ahead in a for loop?
July 21, 2017 -

I'm writing some code and when a specific condition is met inside of the for loop I want the index to be advanced. However, the following code doesn't seem to be doing that.

for i in range(0, n-2):
        print (i)
        if condition == True:
            i = i + 3

I ran it and it just prints all the values of i in order. I also added a print statement in the if condition to make sure that it was evaluating to true.

Why can't I skip ahead? Is that something that Python just doesn't support?

๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Iterating list in "for" loop, with deletions, skips members - Python Help - Discussions on Python.org
January 8, 2022 - I create the list and print one member at a time and delete each. Why is it skipping all the odd numbers? code L = [1,2,3,4,5,6,7,8] for x in L: print(x) L.remove(x) print(L) output 1 [2, 3, 4, 5, 6, 7, 8] 3 [2, 4, 5, 6, 7, 8] 5 [2, 4, 6, 7, 8] 7 [2, 4, 6, 8] ยท It is skipping the even (not ...
๐ŸŒ
Reddit
reddit.com โ€บ r/programminglanguages โ€บ if `continue` is used to skip the current iteration of a loop, then why not call it `skip`?
If `continue` is used to skip the current iteration of a loop, then why not call it `skip`? : r/ProgrammingLanguages
February 12, 2024 - The thing you are "continuing" isn't the current iteration of the loop; it's the sequence of iterations. Using skip would be confusing because you aren't actually skipping the current iteration, nor are you skipping the entire sequence. You could think "you're skipping everything after this point in the current iteration," but that's not always true, depending on the language. For example, consider the following Python code: for i in range(2): try: continue finally: print(i)
๐ŸŒ
LearnDataSci
learndatasci.com โ€บ solutions โ€บ python-continue
Python Continue - Controlling for and while Loops โ€“ LearnDataSci
The square root of 49 is 7.0 The square root of 25 is 5.0 The square root of 36 is 6.0 -9 is negative, so it has been skipped The square root of 4 is 2.0 The square root of 64 is 8.0 -25 is negative, so it has been skipped ... Adding continue here means Python will skip any negative numbers, preventing us from getting a value error. The diagram below shows the process followed inside of our for loop:
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ how-to-skip-iterations-in-a-loop-in-python.html
How to skip iterations in a loop in python?
Description: This query seeks examples demonstrating how to skip iterations within a loop in Python. for i in range(5): if i == 2: continue # Skips iteration when i equals 2 print(i)
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2582454 โ€บ for-loop-iteration-skip
For Loop Iteration Skip | Sololearn: Learn to code for FREE!
j will not become 6 because value ... loop it start again from 0. So if you want to continue second for loop use continue statement instead of break....
๐ŸŒ
DevGenius
blog.devgenius.io โ€บ python-how-can-i-skip-an-iteration-in-a-for-loop-if-it-has-an-error-eb072c3aaa84
Python | how can I skip an iteration in a for loop if it has an error | by Moamen Elabd | Dev Genius
May 17, 2023 - Easiest way to solve this issue is to create a try | except lines in the for loop itself, so when the For loop will run it will try the code there but if has an error, instead of breaking the loop you can try another code or Just pass the iteration