Loops and Ifs are common constructs in programming with python and most other languages. Today we’re going to talk a bit about how to skip a value in a list when using Python. This really comes down to the loop construct, the if construct and you can also include ranges as a solution here.
Wait, what is a loop?
OK, if you are new to programming, then maybe you haven’t used a loop before. So let’s just have a brief tutorial on a loop. A loop is a programming construct that allows you to do something repeatedly.

Types of Loops
Python has a few types of loops. For loops and While loops are the main types, but there are also things called list comprehensions which we’ll cover in a different post. Here is an example of a for loop over a list of words.
For In Loop
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
# let's print each item in our grocery list
for item in groceries:
print(f"Please add {item} to the cart")
As you can see above, the for in loop is just the word for
with the variable you are going to use for each thing in the loop (we are calling it item
), then the word in
and then the variable containing the items groceries
, followed by a colon. Now everything at the level under this is what will happen repeatedly, unless you skip a value in the list. In our case it’s just the print function and we aren’t going to skip anything.
While Loop
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
i = 0
while i < len(groceries):
print(f"Please add {groceries[i]}")
i += 1
Notice that instead of a for
and in
we are going to use while
and some condition. The condition here is i less than the length of the groceries list. Something to note here is that in Python we are 0 based that means that a list’s first element is actually located by index 0, not index 1. Again, we aren’t going to skip anything yet. Just print each one.
For Loop Using Range
Here we are going to use the for loop construct and combine it with range to get a similar effect of the while loop. This is just a setup so that you can see how to skip a value below.
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
for i in range(0, len(groceries)):
print(f"Please add {groceries[i]}")
Finally, How To Skip a Value in a list in Python
Now that we have loops figured out. What if you need to skip certain items in your list? Let’s dive into a few ways that we can do this. Here is How to skip a value in a list Python.
Using If blocks for How to Skip a value in a list Python
In any of the above loops you can easily skip something by using the if construct.
Here is an example where I want to skip any item that starts with ‘b’.
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
# let's print each item in our grocery list
for item in groceries:
if item.startswith('b'):
continue
print(f"Please add {item} to the cart")
Notice that all we do is check to see if the item has some condition (i.e. starts with a b in this case) and then we ‘continue’ which means that we go back to the top of the loop for the next item. You could have just left the continue out and switched the sense of the conditional check like this.
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
# let's print each item in our grocery list
for item in groceries:
if not item.startswith('b'):
print(f"Please add {item} to the cart")
If you had used the break keyword instead of continue, then the loop would completely end. Break is useful for times when you want to stop processing if you have found something.
As an example, let’s say that you want to just see if the word is in the list, in that case you should break when you find it to save resources and not keep checking when you already know.
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
# let's print each item in our grocery list
for item in groceries:
if item == "coke":
print("You've got the soda!")
break
If you were wanting to make a program that kept track of things as you added them to the cart, you might keep an additional list to indicate what is in the cart already. In that case your ‘if’ check would be to check and see if the item is already in the cart like this.
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
cart = ["butter", "bread"]
# let's print each item in our grocery list
for item in groceries:
if item not in cart:
print(f"Please add {item} to the cart")
Note that it would be more efficient to use a set here because searching through a set is way faster than searching through a list. But we’ll cover more about that later.
Using counters for How to Skip a value in a list Python
If you were trying to skip certain lines while looping you could pick any of these 3 methods and make it work just fine.
Here we’ll skip the first, third, etc (all the even ones).
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
# let's print every other item in our grocery list
counter = 0
for item in groceries:
if counter % 2 == 1:
print(f"Don't forget the {item}")
counter += 1 # we have to add to the counter since we printed
Using while index for how to skip a value in a list
Note in the example while loop above, we already have the index (which is in variable ‘i’) and so you can use that to skip by just adding 2 each time through.
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
i = 0
while i < len(groceries):
print(f"Please add {groceries[i]}")
i += 2
Using ranges for How to Skip a value in a list Python
The counter works fine, but if you were using a range function (like mentioned above), you can tell it to step by 2. Then you could also skip a every other value. Notice that the step is configurable, you could easily skip every 5 or what ever you want.
groceries = ["butter", "milk", "eggs", "coke", "pickles", "bread"]
for i in range(0, len(groceries), 2):
print(f"Please add {groceries[i]}")
Summary on loops and skipping stuff
Hopefully this brief article makes it pretty clear how to skip a value in a list while using Python. If you want more info on loops in python you can check out Loops – Learn Python