How to skip Iterations in For Loop Python (original) (raw)
Last Updated : 18 Nov, 2024
Loops are generally used to repeat the tasks. Sometimes, we may want to skip certain steps in the loop. We can do this easily with the continue statement. This article explains how to skip iterations in a for loop.
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.
Python `
a = ["Geeks", "for", "Geeks_2", "Geeks_3"]
for i in a: if i == "for": # Skip "for" continue print(i)
`
Output
Geeks Geeks_2 Geeks_3
**Example #2 - Skipping Odd Numbers
Python `
for i in range(1, 11):
# Check if the number is odd
if i % 2 != 0:
continue
print(i)
`
Using If-else [Alternative to continue]
We can Use _if-else when both _if condition and _else block need to execute distinct logic.
Python `
using if..else to skip iteration
for num in range(1, 6): if num % 2 == 0: # Skip even numbers pass else: print(num)
`
Can we use _break to skip iterations in a loop?
The break statement is used to stop the loop completely. It does not skip iterations. If you want to skip an iteration, you should use continue statement.
Similar Reads
- How to skip Iterations in For Loop - Python Loops are generally used to repeat the tasks. Sometimes, we may want to skip certain steps in the loop. We can do this easily with the continue statement. This article explains how to skip iterations in a for loop. In a for loop, you can use continue to skip a specific iteration when a condition is 2 min read
- Python - Pair iteration in list Pair iteration involves accessing consecutive or specific pairs of elements from a list. It is a common task particularly in scenarios such as comparing neighboring elements, creating tuple pairs, or analyzing sequential data. Python provides several ways to iterate through pairs efficiently ranging 3 min read
- Backward iteration in Python Backward iteration in Python is traversing a sequence (like list, string etc.) in reverse order, moving from the last element to the first. Python provides various methods for backward iteration, such as using negative indexing or employing built-in functions like reversed(). Using reversed() method 2 min read
- How to Input a List in Python using For Loop Using a for loop to take list input is a simple and common method. It allows users to enter multiple values one by one, storing them in a list. This approach is flexible and works well when the number of inputs is known in advance. Let’s start with a basic way to input a list using a for loop in Pyt 2 min read
- How to Iterate Float List in Python We are given a list of floats and our task is to iterate the list of floats and print the result. In this article, we will see how to iterate the float list in Python. Example: Input: float_list = [3.14, 2.718, 1.618, 0.707]Output: Using for loop:3.142.7181.6180.707Explanation: Here, we are iteratin 3 min read
- Iterate Over a List of Lists in Python We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements. For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on. Using Nest 2 min read
- How to Modify a List While Iterating in Python Modifying a list while iterating can be tricky but there are several ways to do it safely. One simple way to modify a list while iterating is by using a for loop with the index of each item. This allows us to change specific items at certain positions. [GFGTABS] Python a = [1, 2, 3, 4] for i in rang 2 min read
- Index Specific Cyclic Iteration in List - Python When working with lists in Python, there may be cases where we need to iterate over a list starting from a specific index in a cyclic manner. For example, given the list [10, 20, 30, 40, 50], we may want to start iterating from index 2 (i.e., 30) and continue iterating in a cyclic manner. There are 3 min read
- Iterate over words of a String in Python In this article, we’ll explore different ways to iterate over the words in a string using Python. Let's start with an example to iterate over words in a Python string: [GFGTABS] Python s = "Learning Python is fun" for word in s.split(): print(word) [/GFGTABS]OutputLearning Python is fun Ex 2 min read
- Python | Alternate Rear iteration The iteration of numbers is done by looping techniques in python. There are many techniques in Python which facilitate looping. Sometimes we require to perform the looping backwards in alternate way and having shorthands to do so can be quite useful. Let’s discuss certain ways in which this can be d 3 min read