Python (Scripting) - Move onto the next item in a loop using continue

by
Jeremy Canfield |
Updated: October 05 2024
| Python (Scripting) articles
- break is used to break out of a loop
- continue is used to move onto the next item in a loop
- pass is used to not run code in a try / except / else / finally error handling
Let's say you have the following to loop through the items in the fruits list.
#!/usr/bin/python
fruits = ["apple", "banana", "orange", "grapes"]
for item in fruits:
print(item)
Running this script will return the following. Each value in the list is printed.
apple
banana
orange
grapes
In this example, continue is used to move onto the next item in the fruits list.
#!/usr/bin/python
fruits = ["apple", "banana", "orange", "grapes"]
for item in fruits:
if item == 'orange':
continue
print(item)
In this example, orange will not be printed.
apple
banana
grapes
Did you find this article helpful?
If so, consider buying me a coffee over at