How to Skip Iterations in a Python For Loop

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.

Python Skip Iterations For Loop

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.

python for loop skip iteration

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)
python for loop skip iteration

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 ifelse 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]

5. 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

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing How to Skip Iterations in a Python For Loop