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 be required to skip a current part of the python for loop and go for the next execution without exiting from the loop, python allows a continue statement to overcome such situations.
Using continue and break statements we can skip the current execution and exit the for & while loops respectively.
In this article, I will explain the usage of the continue statement and using this statement how we can skip the current iteration in a for loop with multiple examples.
2. Skip Iterations in For Loop in Python
The Python continue statement is used in a loop (for or while) to skip the current iteration and move on to the next iteration. It is used to skip when a certain condition is satisfied and move on to the next iteration of the loop. The code following the continue statement in the current iteration is ignored, and the next iteration starts immediately.

Following are some examples of skipping the iteration of for loop.
# Skip the loop using continue statement
courses=["java","python","pandas","sparks"]
for x in courses:
if x == 'pandas':
continue
print(x)
Here, I have taken courses as a list, which is iterated using for loop with the continue statement. As you see continue statement is used within the if condition. If the loop reaches the 'pandas', the condition in the if statement becomes true, so the continue statement will execute and skip the current iteration(pandas) and go for the next iteration.
Note: continue statement doesn’t exit the for/while loop.
The above example yields the below output. Notice that the pandas element is not displayed as it’s been skipped.
Let’s take another example, here I will iterate a sequence of numbers that are generated from the Python range() function. Using the continue statement I skip the number ‘5’ from a sequence. If the loop reaches the '5', the condition in the if statement becomes true, so the continue statement executes and skip the current iteration(5) and go for the next iteration.
# Skip the iteration
# Using range() with continue
for i in range(10):
if i == 5:
continue
print(i)
Yields below output.
3. Skip Iteration When Exception Occurs
In this example, we can skip the iteration in a for loop using a try-except statement with a continue statement. We know that when we divide any number by zero in Python exception will raise as a ZeroDivisionError. We can overcome this problem by using exception handling with the try-except statements and continue statement. Let’s handle the exception of zero division error using the continue statements.
# Skip the iteration using try & except
list1 = [5, 10, 20, 25, 30]
list2 = [5, 10, 0, 5, 3]
list3 = []
for i, j in enumerate(list1):
try:
list3.append(j/list2[i])
except:
continue
print(list3)
# Output:
# [1.0, 1.0, 5.0, 10.0]
4. Skip Iteration in For Loop using Continue with if-else Statement
Using if–else statements with continue statements we can skip the iteration in a loop. Here, I will use the same example as the above but only the difference is I can handle the exception using if-else statements with continue statements. For example,
# skip iteration in For Loop using Continue with if-else
list1 = [5, 10, 20, 25, 30]
list2 = [5, 10, 0, 5, 3]
list3 = []
for i, j in enumerate(list1):
if list2[i] != 0:
list3.append(j/list2[i])
else:
continue
print(list3)
# Output:
# [1.0, 1.0, 5.0, 10.0]
Frequently Asked Questions
To skip iterations based on a condition in a Python for loop, you can use the continue statement. The continue statement is used to skip the rest of the code inside the loop for the current iteration and move on to the next iteration.
You can use the continue statement within an if-else statement in a for loop. The continue statement can be used to skip the rest of the code in the loop and move to the next iteration when a specific condition is met.
To skip iterations when an exception occurs in a for loop, you can use a try-except block. For example, the int(num) conversion inside the try block may raise a ValueError if the element in the list cannot be converted to an integer. The except block catches this exception, prints a message, and then executes the continue statement, skipping the rest of the loop for the current iteration.
It is definitely possible to skip multiple conditions in a for loop using the continue statement. You can use multiple if statements to check various conditions, and if any of those conditions are met, you can use continue to skip the current iteration and move to the next one.
Conclusion
In this article, I have explained the usage of continue statement and using this statement how we can skip the current iteration in a for loop with multiple examples. And also explained exception handling using continue statement with the help of try-except and if-else statements.
Related Articles
- For Loop with If Statement in Python
- For Loop Break Statement in Python
- For Loop Iterate Over an Array in Python
- For Loop Continue And Break in Python
- How to Increment for Loop in Python
- For Loop Enumerate in Python
- How to terminate a script in Python?
- Access Index in for Loop in Python
- How to Start Python For Loop at 1
- Use For Loop to Iterate Tuple in Python
- How to Write Python For Loop in One Line?
- Get Counter Values in a for Loop in Python?