Kodeclik Blog


How to skip values in a Python list

A Python list contains a sequence of items and it is very common to write programs to process the elements in the list. In this blog post, we'll look at how you can use Python to skip certain values when looping through a list. There are four ways to skip values in a list and we will look at these elements in turn.
First let us create a list:
Note that this list contains five elements. Two of these elements are integers, namely 1 and 2592. Two of these elements are strings, namely “Kodeclik” and “Academy”. One of the elements is itself a list, namely [2,3].
If we print this list:
we get:

Looping through the elements of the list

It is easy to loop through the elements using a for loop like so:
The output will be:
Thus, the for loop prints all elements, one element to a line. Now, let us write programs to skip specific elements in such a for loop.

Skip a value in a Python list by position

First, let us learn to skip elements by position. For instance, let us suppose we desire to skip the second element (counting from 1, but recall that indices in Python lists begin from 0). Let us write a function for this purpose:
The function “skip_by_position” takes two elements: list and position (pos). We loop through the elements of this list using the python enumerate function that lists both the index (in variable p) and value (in variable x). For positions not equal to the desired position we add them to the result (i.e., we do not skip it).
Let us apply this function on our example list:
The output will be:
Note that the second element which is the list [2,3] has been skipped.

Skipping multiple values in a Python list by position

We can extend the program above to skip multiple values, i.e., instead of passing a single position, we pass multiple positions in a list:
Note that the second argument is the list called “positions”. Only the “if” statement is modified: instead of checking for equality of position we check for membership in the positions list.
Let us try out our program:
The output is, as expected:
because we removed items at positions 1, 2, and 5 from the list.
The advantage of the program above is that we can pass nonsensical positions in our list and the program will still work:
The output is still:

Skip an entry in a Python list by value

The second type of skipping is when we desire to skip a specific value. For instance, let us skip the value “Academy” from our list. Here is a function to skip by value:
Because we are skipping by value, we don’t quite need the enumerate function. We go through elements of this list in a for loop and check if the element is not equal to the to-be-skipped value and, only if it is not equal we add it to the answer list.
Let us try this out:
The result is:

Skipping multiple entries in a Python list by value

Just like we adapted our first program, let us adapt our skip_by_value function to take a list of values instead of a single value:
Here we are essentially subtracting one list from another (i.e., list-valuelist). For each value in list, we check if it is not in valuelist and only then include it in the answer:
Let us try our function out:
The output is:
Again, like our previous function, we can include nonsensical values to be removed and the program will still work. For instance:
will yield:

Skip an entry in a Python list by type

A third type of skipping is by a type, e.g., let us say we want to skip elements of the list that are lists themselves. Or perhaps we might wish to skip elements that are integers. To see how to do this, it is helpful to familiarize ourselves with the “type” function that returns the type of its operand:
The output will be:
You can see that the first and last elements are integers (of type “int”), and there are two strings and one list. We can use such information to define a criterion to skip. For instance consider the following function:
Just as in our previous examples, we have a list as input and the second argument is an “example of the type to be skipped”. Inside the for loop we check if the type of the element is the same as the type of the second argument and if so we skip it. This is very powerful because we can specify indirectly the nature of what is to be skipped without worrying about how Python represents it internally.
Consider for instance:
Here we are saying we desire to skip elements that are “like” 3, i.e., which are integers. In this case, we have only two integers, namely 1 and 2592. (Recall that [2,3] is of type list of integers, not integers). The output is as expected:
If we try:
the output will be:
i.e., the full list is retained without any removals because 3.5 is of type “float” and there are no floating point elements in the list.
Similarly, if we try:
The output is:
i.e., the only list we have is removed.

Skipping a value in a Python list by property

In general, the above examples should suggest to you that as long as you have a way to check if an entry satisfies a property or not, you can use it in a condition and skip it. So you could adapt the above programs to, for instance, skip even numbers, or say skip strings that denote months, e.g., “January”, “February”, etc. We will leave this as an exercise to the reader.
For closely related content, checkout our blogpost on nested lists in Python. Also learn how to apply a function to all elements of a Python list.
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2023. All rights reserved.