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:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
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:
print(mylist)
we get:
[1, [2, 3], 'Kodeclik', 'Academy', 2592]
Looping through the elements of the list
It is easy to loop through the elements using a for loop like so:
for i in mylist:
print(i)
The output will be:
1
[2, 3]
Kodeclik
Academy
2592
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:
def skip_by_position(list,pos):
newlist = []
for (p,x) in enumerate(list):
if (p+1 != pos):
newlist.append(x)
return(newlist)
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:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
print(skip_by_position(mylist,2))
The output will be:
[1, 'Kodeclik', 'Academy', 2592]
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:
def skip_by_position(list,positions):
newlist = []
for (p,x) in enumerate(list):
if (p+1 not in positions):
newlist.append(x)
return(newlist)
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:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
remove_positions = [1,2,5]
print(skip_by_position(mylist, remove_positions))
The output is, as expected:
['Kodeclik', 'Academy']
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:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
remove_positions = [1,2,5,53]
print(skip_by_position(mylist, remove_positions))
The output is still:
['Kodeclik', 'Academy']
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:
def skip_by_value(list,val):
newlist = []
for x in list:
if (x != val):
newlist.append(x)
return(newlist)
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:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
print(skip_by_value(mylist,"Academy"))
The result is:
[1, [2, 3], 'Kodeclik', 2592]
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:
def skip_by_value(list,valuelist):
newlist = []
for x in list:
if (x not in valuelist):
newlist.append(x)
return(newlist)
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:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
print(skip_by_value(mylist,[1,[2,3]]))
The output is:
['Kodeclik', 'Academy', 2592]
Again, like our previous function, we can include nonsensical values to be removed and the program will still work. For instance:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
print(skip_by_value(mylist,[1,[2,3],"Dinosaur"]))
will yield:
['Kodeclik', 'Academy', 2592]
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:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
for i in mylist:
print(type(i))
The output will be:
<class 'int'>
<class 'list'>
<class 'str'>
<class 'str'>
<class 'int'>
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:
def skip_by_type(list,example_of_type):
newlist = []
for x in list:
if (type(x) != type(example_of_type)):
newlist.append(x)
return(newlist)
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:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
print(skip_by_type(mylist,3))
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:
[[2, 3], 'Kodeclik', 'Academy']
If we try:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
print(skip_by_type(mylist,3.5))
the output will be:
[1, [2, 3], 'Kodeclik', 'Academy', 2592]
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:
mylist = [1, [2,3], "Kodeclik", "Academy", 2592]
print(skip_by_type(mylist,[5,7]))
The output is:
[1, 'Kodeclik', 'Academy', 2592]
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.